fork/home-manager

git clone https://git.julia.blue/fork/home-manager

Robert HelgessonMerge PR #421575790441

master
6.5 KiB232 linesraw
1{ config, lib, pkgs, ... }:
2
3with lib;
4
5let
6
7  cfg = config.nix;
8
9  nixPackage = cfg.package;
10
11  isNixAtLeast = versionAtLeast (getVersion nixPackage);
12
13  nixConf = assert isNixAtLeast "2.2";
14    let
15
16      mkValueString = v:
17        if v == null then
18          ""
19        else if isInt v then
20          toString v
21        else if isBool v then
22          boolToString v
23        else if isFloat v then
24          floatToString v
25        else if isList v then
26          toString v
27        else if isDerivation v then
28          toString v
29        else if builtins.isPath v then
30          toString v
31        else if isString v then
32          v
33        else if isConvertibleWithToString v then
34          toString v
35        else
36          abort "The nix conf value: ${toPretty { } v} can not be encoded";
37
38      mkKeyValue = k: v: "${escape [ "=" ] k} = ${mkValueString v}";
39
40      mkKeyValuePairs = attrs:
41        concatStringsSep "\n" (mapAttrsToList mkKeyValue attrs);
42
43    in pkgs.writeTextFile {
44      name = "nix.conf";
45      text = ''
46        # WARNING: this file is generated from the nix.settings option in
47        # your Home Manager configuration at $XDG_CONFIG_HOME/nix/nix.conf.
48        # Do not edit it!
49        ${mkKeyValuePairs cfg.settings}
50        ${cfg.extraOptions}
51      '';
52      checkPhase =
53        if pkgs.stdenv.hostPlatform != pkgs.stdenv.buildPlatform then ''
54          echo "Ignoring validation for cross-compilation"
55        '' else ''
56          echo "Validating generated nix.conf"
57          ln -s $out ./nix.conf
58          set -e
59          set +o pipefail
60          NIX_CONF_DIR=$PWD \
61            ${cfg.package}/bin/nix show-config ${
62              optionalString (isNixAtLeast "2.3pre")
63              "--no-net --option experimental-features nix-command"
64            } \
65            |& sed -e 's/^warning:/error:/' \
66            | (! grep '${
67              if cfg.checkConfig then "^error:" else "^error: unknown setting"
68            }')
69          set -o pipefail
70        '';
71    };
72
73  semanticConfType = with types;
74    let
75      confAtom = nullOr (oneOf [ bool int float str path package ]) // {
76        description =
77          "Nix config atom (null, bool, int, float, str, path or package)";
78      };
79    in attrsOf (either confAtom (listOf confAtom));
80
81  jsonFormat = pkgs.formats.json { };
82
83in {
84  options.nix = {
85    enable = mkEnableOption ''
86      the Nix configuration module
87    '' // {
88      default = true;
89      visible = false;
90    };
91
92    package = mkOption {
93      type = types.nullOr types.package;
94      default = null;
95      example = literalExpression "pkgs.nix";
96      description = ''
97        The Nix package that the configuration should be generated for.
98      '';
99    };
100
101    registry = mkOption {
102      type = types.attrsOf (types.submodule (let
103        inputAttrs = types.attrsOf
104          (types.oneOf [ types.str types.int types.bool types.package ]);
105      in { config, name, ... }: {
106        options = {
107          from = mkOption {
108            type = inputAttrs;
109            example = {
110              type = "indirect";
111              id = "nixpkgs";
112            };
113            description = "The flake reference to be rewritten.";
114          };
115          to = mkOption {
116            type = inputAttrs;
117            example = {
118              type = "github";
119              owner = "my-org";
120              repo = "my-nixpkgs";
121            };
122            description =
123              "The flake reference to which {option}`from>` is to be rewritten.";
124          };
125          flake = mkOption {
126            type = types.nullOr types.attrs;
127            default = null;
128            example = literalExpression "nixpkgs";
129            description = ''
130              The flake input to which {option}`from>` is to be rewritten.
131            '';
132          };
133          exact = mkOption {
134            type = types.bool;
135            default = true;
136            description = ''
137              Whether the {option}`from` reference needs to match exactly. If set,
138              a {option}`from` reference like `nixpkgs` does not
139              match with a reference like `nixpkgs/nixos-20.03`.
140            '';
141          };
142        };
143        config = {
144          from = mkDefault {
145            type = "indirect";
146            id = name;
147          };
148          to = mkIf (config.flake != null) ({
149            type = "path";
150            path = config.flake.outPath;
151          } // lib.filterAttrs (n: v:
152            n == "lastModified" || n == "rev" || n == "revCount" || n
153            == "narHash") config.flake);
154        };
155      }));
156      default = { };
157      description = ''
158        User level flake registry.
159      '';
160    };
161
162    registryVersion = mkOption {
163      type = types.int;
164      default = 2;
165      internal = true;
166      description = "The flake registry format version.";
167    };
168
169    checkConfig = mkOption {
170      type = types.bool;
171      default = true;
172      description = ''
173        If enabled (the default), checks for data type mismatches and that Nix
174        can parse the generated nix.conf.
175      '';
176    };
177
178    extraOptions = mkOption {
179      type = types.lines;
180      default = "";
181      example = ''
182        keep-outputs = true
183        keep-derivations = true
184      '';
185      description = "Additional text appended to {file}`nix.conf`.";
186    };
187
188    settings = mkOption {
189      type = types.submodule { freeformType = semanticConfType; };
190      default = { };
191      example = literalExpression ''
192        {
193          use-sandbox = true;
194          show-trace = true;
195          system-features = [ "big-parallel" "kvm" "recursive-nix" ];
196        }
197      '';
198      description = ''
199        Configuration for Nix; see {manpage}`nix.conf(5)` for available options.
200        The value declared here will be translated directly to the key-value pairs Nix expects.
201
202        Configuration specified in [](#opt-nix.extraOptions) will be appended
203        verbatim to the resulting config file.
204      '';
205    };
206  };
207
208  config = mkIf cfg.enable (mkMerge [
209    (mkIf (cfg.registry != { }) {
210      xdg.configFile."nix/registry.json".source =
211        jsonFormat.generate "registry.json" {
212          version = cfg.registryVersion;
213          flakes =
214            mapAttrsToList (n: v: { inherit (v) from to exact; }) cfg.registry;
215        };
216    })
217
218    (mkIf (cfg.settings != { } || cfg.extraOptions != "") {
219      assertions = [{
220        assertion = cfg.package != null;
221        message = ''
222          A corresponding Nix package must be specified via `nix.package` for generating
223          nix.conf.
224        '';
225      }];
226
227      xdg.configFile."nix/nix.conf".source = nixConf;
228    })
229  ]);
230
231  meta.maintainers = [ maintainers.polykernel ];
232}