From 223a4a17a4087189167f7d3cf2f5af7b526524f9 Mon Sep 17 00:00:00 2001 From: Thiago Kenji Okada Date: Wed, 13 Oct 2021 13:42:12 -0300 Subject: [PATCH] nixpkgs-disabled: add module This commit introduces the `nixpkgs-disabled` module, that is basically a mock of `nixpkgs` module where any value different from `null` will cause an assertion error. This is to help debugging cases where `home-manager.useGlobalPkgs` is set to `true` and `nixpkgs.*` options are being used. Nowadays this returns the following error: ``` error: The option `home-manager.users..nixpkgs` does not exist. ``` This will change too: ``` error: `nixpkgs` options are disabled when `home-manager.useGlobalPkgs` is enabled. ``` That will direct the user to the correct solution (either removing `nixpkgs` or disable `home-manager.useGlobalPkgs`). --- .github/CODEOWNERS | 2 + modules/misc/nixpkgs-disabled.nix | 73 +++++++++++++++++++++++++++++++ modules/modules.nix | 3 +- 3 files changed, 77 insertions(+), 1 deletion(-) create mode 100644 modules/misc/nixpkgs-disabled.nix diff --git a/.github/CODEOWNERS b/.github/CODEOWNERS index ea1574366..1cce98348 100644 --- a/.github/CODEOWNERS +++ b/.github/CODEOWNERS @@ -19,6 +19,8 @@ /modules/misc/news.nix @rycee +/modules/misc/nixpkgs-disabled.nix @thiagokokada + /modules/misc/numlock.nix @evanjs /tests/modules/misc/numlock @evanjs diff --git a/modules/misc/nixpkgs-disabled.nix b/modules/misc/nixpkgs-disabled.nix new file mode 100644 index 000000000..ab0f35df7 --- /dev/null +++ b/modules/misc/nixpkgs-disabled.nix @@ -0,0 +1,73 @@ +{ config, lib, pkgs, ... }: + +with lib; + +let + + cfg = config.nixpkgs; + + # Copied from nixpkgs.nix. + isConfig = x: builtins.isAttrs x || builtins.isFunction x; + + # Copied from nixpkgs.nix. + optCall = f: x: if builtins.isFunction f then f x else f; + + # Copied from nixpkgs.nix. + mergeConfig = lhs_: rhs_: + let + lhs = optCall lhs_ { inherit pkgs; }; + rhs = optCall rhs_ { inherit pkgs; }; + in lhs // rhs // optionalAttrs (lhs ? packageOverrides) { + packageOverrides = pkgs: + optCall lhs.packageOverrides pkgs + // optCall (attrByPath [ "packageOverrides" ] ({ }) rhs) pkgs; + } // optionalAttrs (lhs ? perlPackageOverrides) { + perlPackageOverrides = pkgs: + optCall lhs.perlPackageOverrides pkgs + // optCall (attrByPath [ "perlPackageOverrides" ] ({ }) rhs) pkgs; + }; + + # Copied from nixpkgs.nix. + configType = mkOptionType { + name = "nixpkgs-config"; + description = "nixpkgs config"; + check = x: + let traceXIfNot = c: if c x then true else lib.traceSeqN 1 x false; + in traceXIfNot isConfig; + merge = args: fold (def: mergeConfig def.value) { }; + }; + + # Copied from nixpkgs.nix. + overlayType = mkOptionType { + name = "nixpkgs-overlay"; + description = "nixpkgs overlay"; + check = builtins.isFunction; + merge = lib.mergeOneOption; + }; + +in { + meta.maintainers = with maintainers; [ thiagokokada ]; + + options.nixpkgs = { + config = mkOption { + default = null; + type = types.nullOr configType; + visible = false; + }; + + overlays = mkOption { + default = null; + type = types.nullOr (types.listOf overlayType); + visible = false; + }; + }; + + config = { + assertions = [{ + assertion = cfg.config == null || cfg.overlays == null; + message = '' + `nixpkgs` options are disabled when `home-manager.useGlobalPkgs` is enabled. + ''; + }]; + }; +} diff --git a/modules/modules.nix b/modules/modules.nix index e7dc912c9..337951abc 100644 --- a/modules/modules.nix +++ b/modules/modules.nix @@ -242,7 +242,8 @@ let ./xsession.nix (pkgs.path + "/nixos/modules/misc/assertions.nix") (pkgs.path + "/nixos/modules/misc/meta.nix") - ] ++ optional useNixpkgsModule ./misc/nixpkgs.nix; + ] ++ optional useNixpkgsModule ./misc/nixpkgs.nix + ++ optional (!useNixpkgsModule) ./misc/nixpkgs-disabled.nix; pkgsModule = { config, ... }: { config = {