forked from edolstra/nix-serve
-
Notifications
You must be signed in to change notification settings - Fork 1
/
service.nix
117 lines (104 loc) · 3.31 KB
/
service.nix
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
{ config, pkgs, lib, ... }:
with pkgs;
with lib;
let
nix-serve-nothing = callPackage ./default.nix { inherit pkgs; };
cfg = config.services.nix-serve-nothing;
mkProxyArgs = nameMapper: proxy:
mapAttrs'
(n: v: nameValuePair
(nameMapper n)
(if (isString v) then (''"${v}"'') else (toString v)))
(filterAttrs
(n: v:
((isString v) && v != "") || (isInt v) || (isType types.float v))
proxy);
mkProxyFlags = proxy:
let
proxyArgs = mapAttrsToList (n: v: "${n}=${v}") (mkProxyArgs toLower proxy);
in
if proxy.enable -> proxyArgs != []
then concatStringsSep " " (["--proxy-opts"] ++ proxyArgs)
else "";
mkProxyEnv = mkProxyArgs (n: "PROXY_${toUpper n}");
in
{
options = {
services.nix-serve-nothing = {
enable = mkEnableOption "nix-serve-nothing, the standalone Nix nothing server. This server will always return 404s.";
port = mkOption {
type = types.int;
default = 5000;
description = ''
Port number where nix-serve-nothing will listen on.
'';
};
bindAddress = mkOption {
type = types.str;
default = "0.0.0.0";
description = ''
IP address where nix-serve-nothing will bind its listening socket.
'';
};
proxy = mkOption {
description = ''
Options to make the server proxy requests to another binary cache
when a check condition is specified.
'';
default = {};
type = types.submodule {
options = {
enable = mkEnableOption "Enable the proxy.";
target = mkOption {
type = types.str;
description = "The address of the proxied binary cache";
default = "";
};
command = mkOption {
type = types.separatedString " ";
description = "The address of the proxied binary cache";
default = "";
};
when = mkOption {
type = types.nullOr types.str;
description = ''
An optional value against which to compare the stdout of proxy.command
'';
};
cachettl = mkOption {
type = types.nullOr types.int;
description = "The number of seconds to wait before checking the command again.";
default = 300;
};
};
};
};
extraParams = mkOption {
type = types.separatedString " ";
default = "";
description = ''
Extra command line parameters for nix-serve-nothing.
'';
};
};
};
config = mkIf cfg.enable {
systemd.services.nix-serve-nothing = {
description = "nix-serve-nothing binary cache server";
after = [ "network.target" ];
wantedBy = [ "multi-user.target" ];
path = [ config.nix.package.out pkgs.bzip2.bin ];
environment = mkProxyEnv cfg.proxy;
serviceConfig = {
Restart = "always";
RestartSec = "5s";
ExecStart = concatStringsSep " "
[ "${nix-serve-nothing}/bin/nix-serve-nothing"
"--listen ${cfg.bindAddress}:${toString cfg.port}"
"${mkProxyFlags cfg.proxy}"
"${cfg.extraParams}" ];
DynamicUser = true;
};
};
};
}