From 1bd171ef6bf50f5ab98d4c726cc9ca986a295938 Mon Sep 17 00:00:00 2001 From: Robert Hensing Date: Wed, 18 May 2022 11:03:42 +0200 Subject: [PATCH] Add pkgs parameter to perSystem --- all-modules.nix | 1 + dev/flake-module.nix | 2 +- modules/nixpkgs.nix | 26 ++++++++++++++++++++ template/default/flake.nix | 5 ++-- template/multi-module/hello/flake-module.nix | 4 +-- 5 files changed, 33 insertions(+), 5 deletions(-) create mode 100644 modules/nixpkgs.nix diff --git a/all-modules.nix b/all-modules.nix index 104553c..e1e4cf9 100644 --- a/all-modules.nix +++ b/all-modules.nix @@ -9,6 +9,7 @@ ./modules/legacyPackages.nix ./modules/nixosConfigurations.nix ./modules/nixosModules.nix + ./modules/nixpkgs.nix ./modules/overlay.nix ./modules/packages.nix ./modules/perSystem.nix diff --git a/dev/flake-module.nix b/dev/flake-module.nix index c1f8146..f4fb450 100644 --- a/dev/flake-module.nix +++ b/dev/flake-module.nix @@ -6,7 +6,6 @@ flakeModuleArgs@{ config, lib, inputs, ... }: ]; systems = [ "x86_64-linux" "aarch64-darwin" ]; perSystem = system: { config, self', inputs', pkgs, ... }: { - _module.args.pkgs = inputs'.nixpkgs.legacyPackages; devShells.default = pkgs.mkShell { nativeBuildInputs = [ @@ -17,6 +16,7 @@ flakeModuleArgs@{ config, lib, inputs, ... }: ${config.pre-commit.installationScript} ''; }; + pre-commit = { inherit pkgs; # should make this default to the one it can get via follows settings = { diff --git a/modules/nixpkgs.nix b/modules/nixpkgs.nix new file mode 100644 index 0000000..76eaf21 --- /dev/null +++ b/modules/nixpkgs.nix @@ -0,0 +1,26 @@ +# +# Nixpkgs module. The only exception to the rule. +# +# Provides a `pkgs` argument in `perSystem`. +# +# Arguably, this shouldn't be in flake-modules-core, but in nixpkgs. +# Nixpkgs could define its own module that does this, which would be +# a more consistent UX, but for now this will do. +# +# The existence of this module does not mean that other flakes' logic +# will be accepted into flake-modules-core, because it's against the +# spirit of Flakes. +# +{ + config = { + perSystem = _: { inputs', lib, ... }: { + config = { + _module.args.pkgs = lib.mkOptionDefault ( + builtins.seq + (inputs'.nixpkgs or (throw "flake-modules-core: The flake does not have a `nixpkgs` input. Please add it, or set `perSystem._module.args.pkgs` yourself.")) + inputs'.nixpkgs.legacyPackages + ); + }; + }; + }; +} diff --git a/template/default/flake.nix b/template/default/flake.nix index 7a6275a..1cb62a3 100644 --- a/template/default/flake.nix +++ b/template/default/flake.nix @@ -19,12 +19,13 @@ ]; systems = [ "x86_64-linux" "aarch64-darwin" ]; - perSystem = system: { config, self', inputs', ... }: { + perSystem = system: { config, self', inputs', pkgs, ... }: { # Per-system attributes can be defined here. The self' and inputs' # module parameters provide easy access to attributes of the same # system. - packages.hello = inputs'.nixpkgs.legacyPackages.hello; + # Equivalent to inputs'.nixpkgs.legacyPackages.hello; + packages.hello = pkgs.hello; }; flake = { # The usual flake attributes can be defined here, including system- diff --git a/template/multi-module/hello/flake-module.nix b/template/multi-module/hello/flake-module.nix index 595ece3..8629bfe 100644 --- a/template/multi-module/hello/flake-module.nix +++ b/template/multi-module/hello/flake-module.nix @@ -1,10 +1,10 @@ # Definitions can be imported from a separate file like this one { self, ... }: { - perSystem = system: { config, self', inputs', ... }: { + perSystem = system: { config, self', inputs', pkgs, ... }: { # Definitions like this are entirely equivalent to the ones # you may have directly in flake.nix. - packages.hello = inputs'.nixpkgs.legacyPackages.hello; + packages.hello = pkgs.hello; }; flake = { nixosModules.hello = { pkgs, ... }: {