2022-01-27 18:58:25 +01:00
|
|
|
{ config, lib, pkgs, ... }:
|
|
|
|
|
|
|
|
with lib;
|
|
|
|
|
|
|
|
let
|
|
|
|
|
|
|
|
cfg = config.nix;
|
|
|
|
|
2022-03-18 03:47:32 +01:00
|
|
|
nixPackage = cfg.package;
|
|
|
|
|
|
|
|
isNixAtLeast = versionAtLeast (getVersion nixPackage);
|
|
|
|
|
|
|
|
nixConf = assert isNixAtLeast "2.2";
|
|
|
|
let
|
|
|
|
|
|
|
|
mkValueString = v:
|
|
|
|
if v == null then
|
|
|
|
""
|
|
|
|
else if isInt v then
|
|
|
|
toString v
|
|
|
|
else if isBool v then
|
|
|
|
boolToString v
|
|
|
|
else if isFloat v then
|
|
|
|
floatToString v
|
|
|
|
else if isList v then
|
|
|
|
toString v
|
|
|
|
else if isDerivation v then
|
|
|
|
toString v
|
|
|
|
else if builtins.isPath v then
|
|
|
|
toString v
|
|
|
|
else if isString v then
|
|
|
|
v
|
2023-06-09 11:57:20 +02:00
|
|
|
else if isConvertibleWithToString v then
|
2022-03-18 03:47:32 +01:00
|
|
|
toString v
|
|
|
|
else
|
|
|
|
abort "The nix conf value: ${toPretty { } v} can not be encoded";
|
|
|
|
|
|
|
|
mkKeyValue = k: v: "${escape [ "=" ] k} = ${mkValueString v}";
|
|
|
|
|
|
|
|
mkKeyValuePairs = attrs:
|
|
|
|
concatStringsSep "\n" (mapAttrsToList mkKeyValue attrs);
|
|
|
|
|
|
|
|
in pkgs.writeTextFile {
|
|
|
|
name = "nix.conf";
|
|
|
|
text = ''
|
|
|
|
# WARNING: this file is generated from the nix.settings option in
|
|
|
|
# your Home Manager configuration at $XDG_CONFIG_HOME/nix/nix.conf.
|
|
|
|
# Do not edit it!
|
|
|
|
${mkKeyValuePairs cfg.settings}
|
|
|
|
${cfg.extraOptions}
|
|
|
|
'';
|
|
|
|
checkPhase =
|
|
|
|
if pkgs.stdenv.hostPlatform != pkgs.stdenv.buildPlatform then ''
|
|
|
|
echo "Ignoring validation for cross-compilation"
|
2024-02-03 11:14:20 +01:00
|
|
|
'' else
|
|
|
|
let
|
|
|
|
showCommand =
|
|
|
|
if isNixAtLeast "2.20pre" then "config show" else "show-config";
|
|
|
|
in ''
|
|
|
|
echo "Validating generated nix.conf"
|
|
|
|
ln -s $out ./nix.conf
|
|
|
|
set -e
|
|
|
|
set +o pipefail
|
|
|
|
NIX_CONF_DIR=$PWD \
|
|
|
|
${cfg.package}/bin/nix ${showCommand} ${
|
|
|
|
optionalString (isNixAtLeast "2.3pre")
|
|
|
|
"--no-net --option experimental-features nix-command"
|
|
|
|
} \
|
|
|
|
|& sed -e 's/^warning:/error:/' \
|
|
|
|
| (! grep '${
|
|
|
|
if cfg.checkConfig then "^error:" else "^error: unknown setting"
|
|
|
|
}')
|
|
|
|
set -o pipefail
|
|
|
|
'';
|
2022-03-18 03:47:32 +01:00
|
|
|
};
|
|
|
|
|
|
|
|
semanticConfType = with types;
|
|
|
|
let
|
|
|
|
confAtom = nullOr (oneOf [ bool int float str path package ]) // {
|
|
|
|
description =
|
|
|
|
"Nix config atom (null, bool, int, float, str, path or package)";
|
|
|
|
};
|
|
|
|
in attrsOf (either confAtom (listOf confAtom));
|
|
|
|
|
|
|
|
jsonFormat = pkgs.formats.json { };
|
|
|
|
|
2022-01-27 18:58:25 +01:00
|
|
|
in {
|
|
|
|
options.nix = {
|
2023-07-02 01:45:18 +02:00
|
|
|
enable = mkEnableOption ''
|
2022-03-18 03:47:32 +01:00
|
|
|
the Nix configuration module
|
2023-07-02 01:45:18 +02:00
|
|
|
'' // {
|
2022-03-18 03:47:32 +01:00
|
|
|
default = true;
|
|
|
|
visible = false;
|
|
|
|
};
|
|
|
|
|
|
|
|
package = mkOption {
|
|
|
|
type = types.nullOr types.package;
|
|
|
|
default = null;
|
|
|
|
example = literalExpression "pkgs.nix";
|
2023-07-02 01:45:18 +02:00
|
|
|
description = ''
|
2022-03-18 03:47:32 +01:00
|
|
|
The Nix package that the configuration should be generated for.
|
|
|
|
'';
|
|
|
|
};
|
|
|
|
|
2022-01-27 18:58:25 +01:00
|
|
|
registry = mkOption {
|
|
|
|
type = types.attrsOf (types.submodule (let
|
|
|
|
inputAttrs = types.attrsOf
|
|
|
|
(types.oneOf [ types.str types.int types.bool types.package ]);
|
|
|
|
in { config, name, ... }: {
|
|
|
|
options = {
|
|
|
|
from = mkOption {
|
|
|
|
type = inputAttrs;
|
|
|
|
example = {
|
|
|
|
type = "indirect";
|
|
|
|
id = "nixpkgs";
|
|
|
|
};
|
2023-07-02 01:45:18 +02:00
|
|
|
description = "The flake reference to be rewritten.";
|
2022-01-27 18:58:25 +01:00
|
|
|
};
|
|
|
|
to = mkOption {
|
|
|
|
type = inputAttrs;
|
|
|
|
example = {
|
|
|
|
type = "github";
|
|
|
|
owner = "my-org";
|
|
|
|
repo = "my-nixpkgs";
|
|
|
|
};
|
2023-07-02 01:45:18 +02:00
|
|
|
description =
|
2023-07-01 01:30:13 +02:00
|
|
|
"The flake reference to which {option}`from>` is to be rewritten.";
|
2022-01-27 18:58:25 +01:00
|
|
|
};
|
|
|
|
flake = mkOption {
|
|
|
|
type = types.nullOr types.attrs;
|
|
|
|
default = null;
|
|
|
|
example = literalExpression "nixpkgs";
|
2023-07-02 01:45:18 +02:00
|
|
|
description = ''
|
2023-07-01 01:30:13 +02:00
|
|
|
The flake input to which {option}`from>` is to be rewritten.
|
2022-01-27 18:58:25 +01:00
|
|
|
'';
|
|
|
|
};
|
|
|
|
exact = mkOption {
|
|
|
|
type = types.bool;
|
|
|
|
default = true;
|
2023-07-02 01:45:18 +02:00
|
|
|
description = ''
|
2023-07-01 01:30:13 +02:00
|
|
|
Whether the {option}`from` reference needs to match exactly. If set,
|
|
|
|
a {option}`from` reference like `nixpkgs` does not
|
|
|
|
match with a reference like `nixpkgs/nixos-20.03`.
|
2022-01-27 18:58:25 +01:00
|
|
|
'';
|
|
|
|
};
|
|
|
|
};
|
|
|
|
config = {
|
|
|
|
from = mkDefault {
|
|
|
|
type = "indirect";
|
|
|
|
id = name;
|
|
|
|
};
|
|
|
|
to = mkIf (config.flake != null) ({
|
|
|
|
type = "path";
|
|
|
|
path = config.flake.outPath;
|
|
|
|
} // lib.filterAttrs (n: v:
|
|
|
|
n == "lastModified" || n == "rev" || n == "revCount" || n
|
|
|
|
== "narHash") config.flake);
|
|
|
|
};
|
|
|
|
}));
|
|
|
|
default = { };
|
2023-07-02 01:45:18 +02:00
|
|
|
description = ''
|
2022-01-27 18:58:25 +01:00
|
|
|
User level flake registry.
|
|
|
|
'';
|
|
|
|
};
|
2022-03-18 03:47:32 +01:00
|
|
|
|
2022-01-27 18:58:25 +01:00
|
|
|
registryVersion = mkOption {
|
|
|
|
type = types.int;
|
|
|
|
default = 2;
|
|
|
|
internal = true;
|
2023-07-02 01:45:18 +02:00
|
|
|
description = "The flake registry format version.";
|
2022-01-27 18:58:25 +01:00
|
|
|
};
|
2022-03-18 03:47:32 +01:00
|
|
|
|
|
|
|
checkConfig = mkOption {
|
|
|
|
type = types.bool;
|
|
|
|
default = true;
|
2023-07-02 01:45:18 +02:00
|
|
|
description = ''
|
2022-03-18 03:47:32 +01:00
|
|
|
If enabled (the default), checks for data type mismatches and that Nix
|
|
|
|
can parse the generated nix.conf.
|
|
|
|
'';
|
|
|
|
};
|
|
|
|
|
|
|
|
extraOptions = mkOption {
|
|
|
|
type = types.lines;
|
|
|
|
default = "";
|
|
|
|
example = ''
|
|
|
|
keep-outputs = true
|
|
|
|
keep-derivations = true
|
|
|
|
'';
|
2023-07-02 01:45:18 +02:00
|
|
|
description = "Additional text appended to {file}`nix.conf`.";
|
2022-03-18 03:47:32 +01:00
|
|
|
};
|
|
|
|
|
|
|
|
settings = mkOption {
|
|
|
|
type = types.submodule { freeformType = semanticConfType; };
|
|
|
|
default = { };
|
|
|
|
example = literalExpression ''
|
|
|
|
{
|
|
|
|
use-sandbox = true;
|
|
|
|
show-trace = true;
|
|
|
|
system-features = [ "big-parallel" "kvm" "recursive-nix" ];
|
|
|
|
}
|
|
|
|
'';
|
2023-07-02 01:45:18 +02:00
|
|
|
description = ''
|
2023-07-01 00:35:51 +02:00
|
|
|
Configuration for Nix; see {manpage}`nix.conf(5)` for available options.
|
2022-03-18 03:47:32 +01:00
|
|
|
The value declared here will be translated directly to the key-value pairs Nix expects.
|
2023-07-01 00:35:51 +02:00
|
|
|
|
|
|
|
Configuration specified in [](#opt-nix.extraOptions) will be appended
|
2022-03-18 03:47:32 +01:00
|
|
|
verbatim to the resulting config file.
|
|
|
|
'';
|
|
|
|
};
|
2022-01-27 18:58:25 +01:00
|
|
|
};
|
|
|
|
|
2023-02-07 11:35:12 +01:00
|
|
|
config = mkIf cfg.enable (mkMerge [
|
|
|
|
(mkIf (cfg.registry != { }) {
|
|
|
|
xdg.configFile."nix/registry.json".source =
|
|
|
|
jsonFormat.generate "registry.json" {
|
2022-03-18 03:47:32 +01:00
|
|
|
version = cfg.registryVersion;
|
|
|
|
flakes =
|
|
|
|
mapAttrsToList (n: v: { inherit (v) from to exact; }) cfg.registry;
|
|
|
|
};
|
2023-02-07 11:35:12 +01:00
|
|
|
})
|
|
|
|
|
|
|
|
(mkIf (cfg.settings != { } || cfg.extraOptions != "") {
|
|
|
|
assertions = [{
|
|
|
|
assertion = cfg.package != null;
|
|
|
|
message = ''
|
|
|
|
A corresponding Nix package must be specified via `nix.package` for generating
|
|
|
|
nix.conf.
|
|
|
|
'';
|
|
|
|
}];
|
2022-03-18 03:47:32 +01:00
|
|
|
|
2023-02-07 11:35:12 +01:00
|
|
|
xdg.configFile."nix/nix.conf".source = nixConf;
|
|
|
|
})
|
|
|
|
]);
|
2022-03-18 03:47:32 +01:00
|
|
|
|
|
|
|
meta.maintainers = [ maintainers.polykernel ];
|
2022-01-27 18:58:25 +01:00
|
|
|
}
|