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