jupyter/flake.nix
2025-07-15 18:40:59 +02:00

162 lines
6.4 KiB
Nix

{
description = "Docker JupyterHub development environment";
inputs = {
nixpkgs.url = "github:NixOS/nixpkgs/nixos-unstable";
flake-utils.url = "github:numtide/flake-utils";
};
outputs = { self, nixpkgs, flake-utils, ... }:
flake-utils.lib.eachDefaultSystem (system:
let
pkgs = nixpkgs.legacyPackages.${system};
app = {
setup-dev = pkgs.writeShellScriptBin "setup-dev" ''
echo "Setting up basic Project Structure"
echo "----------------------------------"
echo "Setting up logs directory"
mkdir -p logs
mkdir -p logs/nginx
mkdir -p logs/jupyterhub
mkdir -p logs/postgres
echo "Logs Directory:"
tree logs/
echo ""
echo "Setting up jupyter-data"
mkdir -p jupyter-data/nbgrader/courses/example-course/{autograded,feedback,release,source/assigment1,submitted}
mkdir -p jupyter-data/nbgrader/{exchange,users}
cat > jupyter-data/nbgrader/courses/example-course/source/assigment1/sample.ipynb << 'EOL'
{
"cells": [
{
"cell_type": "markdown",
"metadata": {},
"source": [
"# Sample Notebook\n",
"## Created with Bash\n",
"\n",
"This Jupyter notebook was generated automatically using a bash script."
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"outputs": [],
"source": [
"# Python code example\n",
"import numpy as np\n",
"import matplotlib.pyplot as plt\n",
"\n",
"x = np.linspace(0, 10, 100)\n",
"y = np.sin(x)\n",
"\n",
"plt.plot(x, y)\n",
"plt.title('Sine Wave')\n",
"plt.show()"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"## Next Steps\n",
"\n",
"- Run the code cells\n",
"- Add more cells\n",
"- Experiment with different Python libraries"
]
}
],
"metadata": {
"kernelspec": {
"display_name": "Python 3",
"language": "python",
"name": "python3"
},
"language_info": {
"codemirror_mode": {
"name": "ipython",
"version": 3
},
"file_extension": ".py",
"mimetype": "text/x-python",
"name": "python",
"nbconvert_exporter": "python",
"pygments_lexer": "ipython3",
"version": "3.8.5"
}
},
"nbformat": 4,
"nbformat_minor": 4
}
EOL
echo "jupyter-data Directory:"
tree jupyter-data
'';
storage = pkgs.writeShellScriptBin "storage" ''
echo "Creating storage"
mkdir -p storage/{jupyter,postgres}
chmod -R 644 storage/
'';
build = pkgs.writeShellScriptBin "build" ''
echo "Building all Containers"
'';
clean = pkgs.writeShellScriptBin "clean" ''
echo "Cleaning Project"
echo "----------------------------------"
echo "Removing logs"
rm -rf logs/
echo "Removing jupyter-data"
rm -rf jupyter-data/
'';
reset = pkgs.writeShellScriptBin "reset" ''
${app.clean}/bin/clean
${app.setup-dev}/bin/setup-dev
'';
};
in
{
devShells.default = pkgs.mkShell {
buildInputs = with pkgs; [
# For deployment/testing
docker
docker-compose
# Terminal Tools
tree
] ++ (builtins.attrValues app);
shellHook = ''
echo "JupyterHub dev shell"
echo "------------------------------------"
echo "$(docker --version)"
echo "------------------------------------"
echo "Available commands:"
echo " setup-dev - Set Up dev environment"
echo " clean - Clean the Project"
echo " reset - Reset project completly (Use with caution it whipes all generated Data)"
'';
};
apps = {
setup-dev = {
type = "app";
program = "${app.setup-dev}/bin/setup-dev";
};
build = {
type = "app";
program = "${app.build}/bin/build";
};
default = self.apps.${system}.dev;
};
});
}