git.iko.soy/typeable/nixpkgs
on master help sign in
nixos/jupyter: init service
commit4d3ce5ca36a145748a4938cb0127c115d8fbacf0 authorAugustin Borsu <a.borsu@gmail.com> dateFri 12 Jan 2018, 00:13 (9 years ago) parent359368f
3 files changed+245
nixos/modules/module-list.nix+1 nixos/modules/services/development/jupyter/default.nix+184 nixos/modules/services/development/jupyter/kernel-options.nix+60
nixos/modules/module-list.nix+1
@@ -250,6 +250,7 @@
250250 ./services/desktops/zeitgeist.nix
251251 ./services/development/bloop.nix
252252 ./services/development/hoogle.nix
253+ ./services/development/jupyter/default.nix
253254 ./services/editors/emacs.nix
254255 ./services/editors/infinoted.nix
255256 ./services/games/factorio.nix
nixos/modules/services/development/jupyter/default.nixnew file+184
@@ -0,0 +1,184 @@
1+{ config, lib, pkgs, ... }:
2+
3+with lib;
4+
5+let
6+
7+ cfg = config.services.jupyter;
8+
9+ # NOTE: We don't use top-level jupyter because we don't
10+ # want to pass in JUPYTER_PATH but use .environment instead,
11+ # saving a rebuild.
12+ package = pkgs.python3.pkgs.notebook;
13+
14+ kernels = (pkgs.jupyter-kernel.create {
15+ definitions = if cfg.kernels != null
16+ then cfg.kernels
17+ else pkgs.jupyter-kernel.default;
18+ });
19+
20+ notebookConfig = pkgs.writeText "jupyter_config.py" ''
21+ ${cfg.notebookConfig}
22+
23+ c.NotebookApp.password = ${cfg.password}
24+ '';
25+
26+in {
27+ meta.maintainers = with maintainers; [ aborsu ];
28+
29+ options.services.jupyter = {
30+ enable = mkEnableOption "Jupyter development server";
31+
32+ ip = mkOption {
33+ type = types.str;
34+ default = "localhost";
35+ description = ''
36+ IP address Jupyter will be listening on.
37+ '';
38+ };
39+
40+ port = mkOption {
41+ type = types.int;
42+ default = 8888;
43+ description = ''
44+ Port number Jupyter will be listening on.
45+ '';
46+ };
47+
48+ notebookDir = mkOption {
49+ type = types.str;
50+ default = "~/";
51+ description = ''
52+ Root directory for notebooks.
53+ '';
54+ };
55+
56+ user = mkOption {
57+ type = types.str;
58+ default = "jupyter";
59+ description = ''
60+ Name of the user used to run the jupyter service.
61+ For security reason, jupyter should really not be run as root.
62+ If not set (jupyter), the service will create a jupyter user with appropriate settings.
63+ '';
64+ example = "aborsu";
65+ };
66+
67+ group = mkOption {
68+ type = types.str;
69+ default = "jupyter";
70+ description = ''
71+ Name of the group used to run the jupyter service.
72+ Use this if you want to create a group of users that are able to view the notebook directory's content.
73+ '';
74+ example = "users";
75+ };
76+
77+ password = mkOption {
78+ type = types.str;
79+ description = ''
80+ Password to use with notebook.
81+ Can be generated using:
82+ In [1]: from notebook.auth import passwd
83+ In [2]: passwd('test')
84+ Out[2]: 'sha1:1b961dc713fb:88483270a63e57d18d43cf337e629539de1436ba'
85+ NOTE: you need to keep the single quote inside the nix string.
86+ Or you can use a python oneliner:
87+ "open('/path/secret_file', 'r', encoding='utf8').read().strip()"
88+ It will be interpreted at the end of the notebookConfig.
89+ '';
90+ example = [
91+ "'sha1:1b961dc713fb:88483270a63e57d18d43cf337e629539de1436ba'"
92+ "open('/path/secret_file', 'r', encoding='utf8').read().strip()"
93+ ];
94+ };
95+
96+ notebookConfig = mkOption {
97+ type = types.lines;
98+ default = "";
99+ description = ''
100+ Raw jupyter config.
101+ '';
102+ };
103+
104+ kernels = mkOption {
105+ type = types.nullOr (types.attrsOf(types.submodule (import ./kernel-options.nix {
106+ inherit lib;
107+ })));
108+
109+ default = null;
110+ example = literalExample ''
111+ {
112+ python3 = let
113+ env = (pkgs.python3.withPackages (pythonPackages: with pythonPackages; [
114+ ipykernel
115+ pandas
116+ scikitlearn
117+ ]));
118+ in {
119+ displayName = "Python 3 for machine learning";
120+ argv = [
121+ "$ {env.interpreter}"
122+ "-m"
123+ "ipykernel_launcher"
124+ "-f"
125+ "{connection_file}"
126+ ];
127+ language = "python";
128+ logo32 = "$ {env.sitePackages}/ipykernel/resources/logo-32x32.png";
129+ logo64 = "$ {env.sitePackages}/ipykernel/resources/logo-64x64.png";
130+ };
131+ }
132+ '';
133+ description = "Declarative kernel config
134+
135+ Kernels can be declared in any language that supports and has the required
136+ dependencies to communicate with a jupyter server.
137+ In python's case, it means that ipykernel package must always be included in
138+ the list of packages of the targeted environment.
139+ ";
140+ };
141+ };
142+
143+ config = mkMerge [
144+ (mkIf cfg.enable {
145+ systemd.services.jupyter = {
146+ description = "Jupyter development server";
147+
148+ wantedBy = [ "multi-user.target" ];
149+
150+ # TODO: Patch notebook so we can explicitly pass in a shell
151+ path = [ pkgs.bash ]; # needed for sh in cell magic to work
152+
153+ environment = {
154+ JUPYTER_PATH = toString kernels;
155+ };
156+
157+ serviceConfig = {
158+ Restart = "always";
159+ ExecStart = ''${package}/bin/jupyter-notebook \
160+ --no-browser \
161+ --ip=${cfg.ip} \
162+ --port=${toString cfg.port} --port-retries 0 \
163+ --notebook-dir=${cfg.notebookDir} \
164+ --NotebookApp.config_file=${notebookConfig}
165+ '';
166+ User = cfg.user;
167+ Group = cfg.group;
168+ WorkingDirectory = "~";
169+ };
170+ };
171+ })
172+ (mkIf (cfg.enable && (cfg.group == "jupyter")) {
173+ users.groups.jupyter = {};
174+ })
175+ (mkIf (cfg.enable && (cfg.user == "jupyter")) {
176+ users.extraUsers.jupyter = {
177+ extraGroups = [ cfg.group ];
178+ home = "/var/lib/jupyter";
179+ createHome = true;
180+ useDefaultShell = true; # needed so that the user can start a terminal.
181+ };
182+ })
183+ ];
184+}
nixos/modules/services/development/jupyter/kernel-options.nixnew file+60
@@ -0,0 +1,60 @@
1+# Options that can be used for creating a jupyter kernel.
2+{lib }:
3+
4+with lib;
5+
6+{
7+ options = {
8+
9+ displayName = mkOption {
10+ type = types.str;
11+ default = "";
12+ example = [
13+ "Python 3"
14+ "Python 3 for Data Science"
15+ ];
16+ description = ''
17+ Name that will be shown to the user.
18+ '';
19+ };
20+
21+ argv = mkOption {
22+ type = types.listOf types.str;
23+ example = [
24+ "{customEnv.interpreter}"
25+ "-m"
26+ "ipykernel_launcher"
27+ "-f"
28+ "{connection_file}"
29+ ];
30+ description = ''
31+ Command and arguments to start the kernel.
32+ '';
33+ };
34+
35+ language = mkOption {
36+ type = types.str;
37+ example = "python";
38+ description = ''
39+ Language of the environment. Typically the name of the binary.
40+ '';
41+ };
42+
43+ logo32 = mkOption {
44+ type = types.nullOr types.path;
45+ default = null;
46+ example = "{env.sitePackages}/ipykernel/resources/logo-32x32.png";
47+ description = ''
48+ Path to 32x32 logo png.
49+ '';
50+ };
51+ logo64 = mkOption {
52+ type = types.nullOr types.path;
53+ default = null;
54+ example = "{env.sitePackages}/ipykernel/resources/logo-64x64.png";
55+ description = ''
56+ Path to 64x64 logo png.
57+ '';
58+ };
59+ };
60+}