summary refs log tree commit diff
path: root/nixos/modules/services/development/livebook.nix
blob: 3991a4125ec39c64c7d8448f45392a4aed776bd0 (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
{ config, lib, pkgs, ... }:

with lib;
let
  cfg = config.services.livebook;
in
{
  options.services.livebook = {
    # Since livebook doesn't have a granular permission system (a user
    # either has access to all the data or none at all), the decision
    # was made to run this as a user service.  If that changes in the
    # future, this can be changed to a system service.
    enableUserService = mkEnableOption "a user service for Livebook";

    environmentFile = mkOption {
      type = types.path;
      description = lib.mdDoc ''
        Environment file as defined in {manpage}`systemd.exec(5)` passed to the service.

        This must contain at least `LIVEBOOK_PASSWORD` or
        `LIVEBOOK_TOKEN_ENABLED=false`.  See `livebook server --help`
        for other options.'';
    };

    erlang_node_short_name = mkOption {
      type = with types; nullOr str;
      default = null;
      example = "livebook";
      description = "A short name for the distributed node.";
    };

    erlang_node_name = mkOption {
      type = with types; nullOr str;
      default = null;
      example = "livebook@127.0.0.1";
      description = "The name for the app distributed node.";
    };

    port = mkOption {
      type = types.port;
      default = 8080;
      description = "The port to start the web application on.";
    };

    address = mkOption {
      type = types.str;
      default = "127.0.0.1";
      description = lib.mdDoc ''
        The address to start the web application on.  Must be a valid IPv4 or
        IPv6 address.
      '';
    };

    options = mkOption {
      type = with types; attrsOf str;
      default = { };
      description = lib.mdDoc ''
        Additional options to pass as command-line arguments to the server.
      '';
      example = literalExpression ''
        {
          cookie = "a value shared by all nodes in this cluster";
        }
      '';
    };
  };

  config = mkIf cfg.enableUserService {
    systemd.user.services.livebook = {
      serviceConfig = {
        Restart = "always";
        EnvironmentFile = cfg.environmentFile;
        ExecStart =
          let
            args = lib.cli.toGNUCommandLineShell { } ({
              inherit (cfg) port;
              ip = cfg.address;
              name = cfg.erlang_node_name;
              sname = cfg.erlang_node_short_name;
            } // cfg.options);
          in
          "${pkgs.livebook}/bin/livebook server ${args}";
      };
      path = [ pkgs.bash ];
      wantedBy = [ "default.target" ];
    };
  };

  meta.doc = ./livebook.md;
}