summaryrefslogtreecommitdiff
path: root/system-profiles/core/default.nix
blob: 71d0619afbb9a1b061fb088eae38c2def5e53069 (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
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
{ flake, flakeInputs, home-manager, path, hostName, config, options, lib, pkgs, customUtils, ... }:

with lib;

let
  profileSet = customUtils.types.attrNameSet flake.nixosModules.systemProfiles;
  userProfileSet = customUtils.types.attrNameSet (zipAttrs (attrValues flake.nixosModules.userProfiles));
  hasSops = config.sops.secrets != {};
in {
  imports =
    [ flakeInputs.sops-nix.nixosModules.sops
      home-manager.nixosModules.home-manager
    ];

  options = {
    # See mkSystemProfile in ../flake.nix
    system.profiles = mkOption {
      type = profileSet;
      default = [];
      description = ''
        Set (list without duplicates) of ‘systemProfiles’ enabled for this host
      '';
    };

    users.users = mkOption {
      type = types.attrsOf (types.submodule {
        options.profiles = mkOption {
          type = userProfileSet;
          default = [];
          description = ''
            Set (list without duplicates) of ‘userProfiles’ enabled for this user
          '';
        };
      });
    };

    nixpkgs.externalConfig = mkOption {
      default = {};
      example = literalExpression
        ''
          { allowBroken = true; allowUnfree = true; }
        '';
      type = mkOptionType {
        name = "nixpkgs-config";
        description = "nixpkgs config";
        check = x:
          let traceXIfNot = c:
                if c x then true
                else traceSeqN 1 x false;
              isConfig = x:
                builtins.isAttrs x || isFunction x;
          in traceXIfNot isConfig;
        merge = args:
          let
            optCall = f: x:
              if isFunction f
              then f x
              else f;
            mergeConfig = lhs_: rhs_:
              let
                lhs = optCall lhs_ { inherit pkgs; };
                rhs = optCall rhs_ { inherit pkgs; };
              in
              recursiveUpdate lhs rhs //
              optionalAttrs (lhs ? packageOverrides) {
                packageOverrides = pkgs:
                  optCall lhs.packageOverrides pkgs //
                  optCall (attrByPath [ "packageOverrides" ] { } rhs) pkgs;
              } //
              optionalAttrs (lhs ? perlPackageOverrides) {
                perlPackageOverrides = pkgs:
                  optCall lhs.perlPackageOverrides pkgs //
                  optCall (attrByPath [ "perlPackageOverrides" ] { } rhs) pkgs;
              };
          in foldr (def: mergeConfig def.value) {};
      };
      description = ''
        The configuration of the Nix Packages collection.  (For
        details, see the Nixpkgs documentation.)  It allows you to set
        package configuration options.

        Used to construct `nixpkgs.pkgs`.
      '';
    };

    nixpkgs = {
      flakeInput = mkOption {
        type = types.enum (attrNames flakeInputs);
        default = if flakeInputs ? "nixpkgs-${hostName}" then "nixpkgs-${hostName}" else "nixpkgs";
        defaultText = literalExpression ''if flakeInputs ? "nixpkgs-''${hostName}" then "nixpkgs-''${hostName}" else "nixpkgs"'';
        internal = true;
      };
      extraOverlays = mkOption {
        default = [];
        type = types.listOf (mkOptionType {
          name = "nixpkgs-overlay";
          description = "nixpkgs overlay";
          check = lib.isFunction;
          merge = lib.mergeOneOption;
        });
      };
    };
  };

  config = foldr recursiveUpdate {} ([
    {
      networking.hostName = hostName;
      system.configurationRevision = mkIf (flake ? rev) flake.rev;

      nixpkgs.pkgs = import (flakeInputs.${config.nixpkgs.flakeInput}.outPath + "/pkgs/top-level") {
        overlays = attrValues flake.overlays ++ config.nixpkgs.extraOverlays;
        config = config.nixpkgs.externalConfig;
        localSystem = config.nixpkgs.system;
      };

      nix = {
        package = if builtins.hasAttr "latest" pkgs.nixVersions then pkgs.nixVersions.latest else pkgs.nixUnstable;
        settings = {
          sandbox = true;
          allowed-users = [ "*" ];
          trusted-users = [ "root" "@wheel" ];

          experimental-features = ["nix-command" "flakes" "auto-allocate-uids" "cgroups"];
          auto-allocate-uids = true;
          use-cgroups = true;
          use-xdg-base-directories = true;

          flake-registry = "${flakeInputs.flake-registry}/flake-registry.json";
        };
        nixPath = [
          "nixpkgs=${pkgs.runCommand "nixpkgs" {} ''
            mkdir $out
            ln -s ${./nixpkgs.nix} $out/default.nix
            ln -s /run/nixpkgs/lib $out/lib
          ''}"
        ];
        registry =
          let override = { self = "nixos"; };
          in mapAttrs' (inpName: inpFlake: nameValuePair
            (override.${inpName} or inpName)
            { flake = inpFlake; } ) flakeInputs;
      };

      systemd.tmpfiles.rules = [
        "L+ /run/nixpkgs - - - - ${flakeInputs.${config.nixpkgs.flakeInput}.outPath}"
        "L+ /run/nixpkgs-overlays.nix - - - - ${pkgs.writeText "overlays.nix" ''
          with builtins;

          attrValues (import
            (
              let lock = fromJSON (readFile ${flake + "/flake.lock"}); in
              fetchTarball {
                url = "https://github.com/edolstra/flake-compat/archive/''${lock.nodes.flake-compat.locked.rev}.tar.gz";
                sha256 = lock.nodes.flake-compat.locked.narHash;
              }
            )
            { src = ${flake}; }
          ).defaultNix.overlays
        ''}"
        "L+ /etc/nixos - - - - ${flake}"
      ] ++ map (input: "L+ /run/flake-inputs/${input} - - - - ${flakeInputs.${input}.outPath}") (attrNames flakeInputs);

      users.mutableUsers = false;

      documentation.nixos = {
        includeAllModules = true;
        options.warningsAreErrors = false;
        extraModuleSources = map toString ([flake] ++ attrValues flakeInputs);
      };

      home-manager = {
        useGlobalPkgs = true; # Otherwise home-manager would only work impurely
        useUserPackages = false;
        useUserService = true;
        backupFileExtension = "bak";
        sharedModules = lib.attrValues flake.homeModules ++ [
          {
            manual.manpages.enable = true;
            systemd.user.startServices = "sd-switch";

            programs.ssh.internallyManaged = mkForce true;
          }
        ];
        extraSpecialArgs = { inherit flake flakeInputs path; };
      };

      sops = mkIf hasSops {
        age = {
          keyFile = "/var/lib/sops-nix/key.txt";
          generateKey = false;
          sshKeyPaths = [];
        };
        gnupg = {
          home = null;
          sshKeyPaths = [];
        };
      };

      programs.git = {
        enable = true;
        lfs.enable = true;
      };
      environment.systemPackages = with pkgs; [ git-annex scutiger ];
    }
  ] ++ (optional (options ? system.switch.enableNg) {
    system.switch = lib.mkDefault {
      enable = false;
      enableNg = true;
    };
  })
  ++ (optional (options ? system.etc) {
    boot.initrd.systemd.enable = lib.mkDefault true;
    system.etc.overlay.enable = lib.mkDefault true;
    system.etc.overlay.mutable = lib.mkDefault (!config.systemd.sysusers.enable);
    systemd.sysusers.enable = lib.mkDefault true;

    # Random perl remnants
    system.disableInstallerTools = lib.mkDefault true;
    programs.less.lessopen = lib.mkDefault null;
    programs.command-not-found.enable = lib.mkDefault false;
    boot.enableContainers = lib.mkDefault false;
    boot.loader.grub.enable = lib.mkDefault false;
    environment.defaultPackages = lib.mkDefault [ ];
    documentation.info.enable = lib.mkDefault false;
  })
  ++ (optional (options ? nixpkgs.flake) {
    nixpkgs.flake = {
      setNixPath = false;
      setFlakeRegistry = false;
    };
  }));
}