LnL7 / nix-darwin

nix modules for darwin
MIT License
3.25k stars 461 forks source link

Add flake support to `darwin-option` #277

Open ashkan-leo opened 3 years ago

ashkan-leo commented 3 years ago

Hello

First, thanks for the fantastic project.

I have installed nix-darwin on my system using flakes. I can rebuild my configurations, but darwin-option does not work. I get the following error:

ashkanaleali@zebra> darwin-option --flake ~/.config/nixpkgs env                                                                                                                                                                             ~/.nix-defexpr
error: --- ThrownError -------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------- nix-instantiate
at: (1:13) from string

     1| with import <darwin> {}; builtins.concatStringsSep "\n" (builtins.attrNames options.env)
      |             ^

file 'darwin' was not found in the Nix search path (add it using $NIX_PATH or -I)

What should darwin be in the NIX_PATH and what is the best place to set it in my configuration files?

LnL7 commented 3 years ago

Yeah, darwin-option wasn't updated to support flakes yet.

lilyball commented 2 years ago

@ashkan-leo https://github.com/gytis-ivaskevicius/flake-utils-plus has a NixOS module (that works in nix-darwin) that can automatically add nix registry and NIX_PATH entries for your flake inputs. Unfortunately it doesn't work for <darwin> yet (see https://github.com/gytis-ivaskevicius/flake-utils-plus/issues/107). In the meantime, you can do this yourself with the nix.nixPath config.

Personally, I went ahead and reimplemented the flake-utils-plus solution (which uses symlinks in /etc/nix/inputs to keep all shells up-to-date on config rebuilds). Here's my module:

modules/nix.nix ```nix { 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" ]; }; } ```

With this, my core config just says

{
  nix.generateRegistryFromInputs = true;
  nix.generateNixPathFromInputs = true;
}

@LnL7 I would really love to see darwin-option learn to use flakes. Perhaps this issue could be repurposed for that?