aboutsummaryrefslogtreecommitdiff
path: root/modules/apps/nix-serve.nix
blob: 20deab60901d8ed6da7bd1f5b18e5c82addade44 (plain)
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
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
{ config, pkgs, lib, ... }:

let

  inherit (lib)
    mkEnableOption mkIf mkOption optionalString ;

  inherit (lib.types)
    int nullOr package path str ;

  cfg = config.nixsap.apps.nix-serve;

  start =
    let
      maybeTCP = optionalString (cfg.port != null)
                   "--listen '${cfg.address}:${toString cfg.port}'";
    in pkgs.writeBashScriptBin "nix-serve" ''
      umask 0117 # for socket mode

      export NIX_REMOTE="daemon"

      ${optionalString (cfg.secretKeyFile != null) ''
        export NIX_SECRET_KEY_FILE='${cfg.secretKeyFile}'
      ''}

      exec "${cfg.package}/bin/nix-serve" \
        ${maybeTCP} \
        --listen '${cfg.socket}' \
        --workers ${toString cfg.workers}
    '';

in
{
  options = {
    nixsap.apps.nix-serve = {
      enable = mkEnableOption "nix-serve, the standalone Nix binary cache server";

      user = mkOption {
        description = "User and group to run as";
        type = str;
        default = "nix-serve";
      };

      home = mkOption {
        description = "Home directory (currently for Unix socket only)";
        type = path;
        default = "/nix-serve";
      };

      package = mkOption {
        description = "nix-serve package";
        type = package;
        default = pkgs.nix-serve;
      };

      workers = mkOption {
        type = int;
        default = 5;
        description = "Specifies the number of worker pool";
      };

      port = mkOption {
        type = nullOr int;
        default = null;
        description = ''
          Port number where nix-serve will listen on in addition to Unix
          socket.  By default nix-serve listens on Unix socket only.
        '';
      };

      address = mkOption {
        type = str;
        default = "127.0.0.1";
        description = ''
          IP address where nix-serve will bind its TCP listening socket.
        '';
      };

      socket = mkOption {
        description = ''
          Unix socket to listen on.
        '';
        readOnly = true;
        type = path;
        default = "${cfg.home}/socket";
      };

      secretKeyFile = mkOption {
        type = nullOr path;
        default = null;
        description = ''
          The path to the file used for signing derivation data.
        '';
      };
    };
  };

  config = mkIf cfg.enable {
    nix.allowedUsers = [ cfg.user ];

    nixsap.deployment.keyrings.${cfg.user} = [ cfg.secretKeyFile ];
    nixsap.system.users.daemons = [ cfg.user ];

    systemd.services.nix-serve = {
      description = "nix-serve binary cache server";
      wantedBy = [ "multi-user.target" ];
      wants = [ "keys.target" ];
      after = [ "keys.target" "network.target" "local-fs.target" ];

      preStart = ''
        mkdir -p -- '${cfg.home}'
        rm -rf -- '${cfg.socket}'
        chown -Rc '${cfg.user}:${cfg.user}' -- '${cfg.home}'
        chmod -Rc u=rwX,g=rX,o= -- '${cfg.home}'
      '';

      serviceConfig = {
        ExecStart = "${start}/bin/nix-serve";
        KillMode = "mixed";
        PermissionsStartOnly = true;
        Restart = "always";
        User = cfg.user;
      };
    };
  };
}