From 6a027c397156f520db4c82c5819afc1f532f5004 Mon Sep 17 00:00:00 2001 From: willemml Date: Mon, 6 Feb 2023 16:42:22 -0800 Subject: [PATCH] fix nix_path for darwin-option --- flake.nix | 7 ++-- modules/nix.nix | 83 +++++++++++++++++++++++++++++++++++++++++++++++ system/darwin.nix | 11 ++++++- 3 files changed, 97 insertions(+), 4 deletions(-) create mode 100644 modules/nix.nix diff --git a/flake.nix b/flake.nix index 2cee535..2fe6d4c 100644 --- a/flake.nix +++ b/flake.nix @@ -32,7 +32,7 @@ pkgs = throw "nixpkgs eval"; }; - home-manager-config = { + user-config = { home-manager.useGlobalPkgs = true; home-manager.useUserPackages = true; home-manager.extraSpecialArgs = { inherit nurNoPkgs pkgs; inputs = { inherit (inputs); }; }; @@ -49,11 +49,12 @@ { darwinConfigurations = { zeus = darwin.lib.darwinSystem { - inherit system pkgs; + inherit system pkgs inputs; modules = [ + ./modules/nix.nix ./system/darwin.nix home-manager.darwinModules.home-manager - home-manager-config + user-config ]; }; }; diff --git a/modules/nix.nix b/modules/nix.nix new file mode 100644 index 0000000..c53886d --- /dev/null +++ b/modules/nix.nix @@ -0,0 +1,83 @@ +# https://github.com/LnL7/nix-darwin/issues/277#issuecomment-992866471 + +{ config, lib, pkgs, inputs, ... }: + +with lib; + +let + cfg = config.nix; + + nixRegistry = builtins.mapAttrs (name: value: { flake = value; }) inputs; + etcNixInputs = pkgs.runCommandNoCC "etc-nix-inputs" + { + inputNames = builtins.attrNames inputs; + inputPaths = builtins.map (x: x.outPath) (builtins.attrValues inputs); + } '' + mkdir -p $out + inputNames=($inputNames) + inputPaths=($inputPaths) + for (( i=0; i<''${#inputNames[@]}; i++)); do + source=''${inputPaths[$i]} + name=''${inputNames[$i]} + if [[ -f $source/default.nix ]]; then + ln -s $source $out/$name + fi + done + ''; +in + +# Based on flake-utils-plus#nixosModules.autoGenFromInputs + # https://github.com/gytis-ivaskevicius/flake-utils-plus/blob/master/lib/options.nix + # + # We're not using that directly because we don't need the rest of the flake, and to work around + # https://github.com/gytis-ivaskevicius/flake-utils-plus/issues/105 and + # https://github.com/gytis-ivaskevicius/flake-utils-plus/issues/107 + +{ + options = { + nix.generateNixPathFromInputs = mkOption { + type = types.bool; + description = '' + If set, NIX_PATH will be generated from available inputs. + This requires `nix.linkInputs` to be enabled, and setting this will default + `nix.linkInputs` to true. + ''; + default = false; + example = true; + }; + nix.generateRegistryFromInputs = mkOption { + type = types.bool; + description = '' + If set, the system Nix registry will be generated from available inputs. + Otherwise, the registry will still include the `self` flake. + ''; + default = false; + example = true; + }; + nix.linkInputs = mkOption { + type = types.bool; + description = "If set, inputs will be symlinked into /etc/nix/inputs."; + example = true; + }; + }; + + config = { + assertions = [{ + assertion = cfg.generateNixPathFromInputs -> cfg.linkInputs; + message = "nix.generateNixPathFromInputs requires nix.linkInputs"; + }]; + + nix.linkInputs = mkDefault cfg.generateNixPathFromInputs; + + nix.registry = + if cfg.generateRegistryFromInputs + then nixRegistry + else { self.flake = inputs.self; }; + + environment.etc."nix/inputs" = mkIf cfg.linkInputs { + source = etcNixInputs; + }; + + nix.nixPath = mkIf cfg.generateNixPathFromInputs [ "/etc/nix/inputs" ]; + }; +} diff --git a/system/darwin.nix b/system/darwin.nix index 4c54fe3..d92940b 100644 --- a/system/darwin.nix +++ b/system/darwin.nix @@ -3,7 +3,16 @@ { environment.systemPackages = with pkgs; [ ]; - nix.package = pkgs.nix; + nix = { + extraOptions = '' + experimental-features = nix-command flakes + extra-trusted-users = willem + ''; + generateRegistryFromInputs = true; + generateNixPathFromInputs = true; + linkInputs = true; + package = pkgs.nix; + }; programs.bash.enable = true;