summaryrefslogtreecommitdiff
path: root/modules/build-client.nix
blob: 9924b751397777e698b5c4a9e11d18d3cf094375 (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
{ flake, config, lib, hostName, ... }:

with lib;

let
  cfg = config.nix.buildServers;

  secretName = name: "nix-ssh-builder-${name}-private";
  buildHostName = host: "nix-ssh-builder-${host}";
in {
  options = {
    nix = {
      buildServers = mkOption {
        type = types.attrsOf (types.submodule {
          options = {
            address = mkOption {
              type = types.str;
            };

            system = mkOption {
              type = types.nullOr types.str;
              default = null;
              example = "x86_64-linux";
              description = ''
                The system type the build machine can execute derivations on.
                Either this attribute or <varname>systems</varname> must be
                present, where <varname>system</varname> takes precedence if
                both are set.
              '';
            };
            systems = mkOption {
              type = types.listOf types.str;
              default = [];
              example = [ "x86_64-linux" "aarch64-linux" ];
              description = ''
                The system types the build machine can execute derivations on.
                Either this attribute or <varname>system</varname> must be
                present, where <varname>system</varname> takes precedence if
                both are set.
              '';
            };
            maxJobs = mkOption {
              type = types.int;
              default = 1;
              description = ''
                The number of concurrent jobs the build machine supports. The
                build machine will enforce its own limits, but this allows hydra
                to schedule better since there is no work-stealing between build
                machines.
              '';
            };
            speedFactor = mkOption {
              type = types.int;
              default = 1;
              description = ''
                The relative speed of this builder. This is an arbitrary integer
                that indicates the speed of this builder, relative to other
                builders. Higher is faster.
              '';
            };
            mandatoryFeatures = mkOption {
              type = types.listOf types.str;
              default = [];
              example = [ "big-parallel" ];
              description = ''
                A list of features mandatory for this builder. The builder will
                be ignored for derivations that don't require all features in
                this list. All mandatory features are automatically included in
                <varname>supportedFeatures</varname>.
              '';
            };
            supportedFeatures = mkOption {
              type = types.listOf types.str;
              default = [];
              example = [ "kvm" "big-parallel" ];
              description = ''
                A list of features supported by this builder. The builder will
                be ignored for derivations that require features not in this
                list.
              '';
            };
          };
        });
        default = {};
      };
    };
  };
  
  config = mkIf (cfg != {}) {
    programs.ssh.extraConfig = concatMapStringsSep "\n" ({ name, value }: ''
      Host ${buildHostName name}
        User nix-ssh-builder
        HostName ${value.address}
        IdentitiesOnly yes
        IdentityFile ${config.sops.secrets.${secretName name}.path}
        ControlMaster auto
        ControlPath /run/nix-ssh-builder-master-%r@%n:%p
        ControlPersist 30m
        Compression yes
        ForwardAgent no
        ServerAliveInterval 6
        ServerAliveCountMax 10
    '') (mapAttrsToList nameValuePair cfg);

    sops.secrets = mapAttrs' (name: hCfg: nameValuePair (secretName name) { sopsFile = ../system-profiles/build-server/clients + "/${hostName}/private"; format = "binary"; }) cfg;

    nix = {
      distributedBuilds = true;
      extraOptions = ''
        builders-use-substitutes = true
      '';
      buildMachines = mapAttrsToList (name: hCfg: { hostName = buildHostName name; inherit (hCfg) system systems maxJobs speedFactor mandatoryFeatures supportedFeatures; }) cfg;
    };
  };
}