modules/nixpkgs: add config option

This commit is contained in:
Matt Sturgeon 2024-12-23 13:42:46 +00:00
parent 2d68ef843a
commit 5bd04ce09a
No known key found for this signature in database
GPG key ID: 4F91844CED1A8299

View file

@ -2,11 +2,34 @@
config, config,
options, options,
lib, lib,
pkgs,
... ...
}: }:
let let
cfg = config.nixpkgs; cfg = config.nixpkgs;
opt = options.nixpkgs; opt = options.nixpkgs;
isConfig = x: builtins.isAttrs x || lib.isFunction x;
mergeConfig =
lhs_: rhs_:
let
optCall = maybeFn: x: if lib.isFunction maybeFn then maybeFn x else maybeFn;
lhs = optCall lhs_ { inherit pkgs; };
rhs = optCall rhs_ { inherit pkgs; };
in
lib.recursiveUpdate lhs rhs
// lib.optionalAttrs (lhs ? packageOverrides) {
packageOverrides =
pkgs:
optCall lhs.packageOverrides pkgs // optCall (lib.attrByPath [ "packageOverrides" ] { } rhs) pkgs;
}
// lib.optionalAttrs (lhs ? perlPackageOverrides) {
perlPackageOverrides =
pkgs:
optCall lhs.perlPackageOverrides pkgs
// optCall (lib.attrByPath [ "perlPackageOverrides" ] { } rhs) pkgs;
};
in in
{ {
options.nixpkgs = { options.nixpkgs = {
@ -56,6 +79,29 @@ in
''; '';
}; };
config = lib.mkOption {
default = { };
example = {
allowBroken = true;
allowUnfree = true;
};
type = lib.mkOptionType {
name = "nixpkgs-config";
description = "nixpkgs config";
check = x: isConfig x || lib.traceSeqN 1 x false;
merge = loc: lib.foldr (def: mergeConfig def.value) { };
};
description = ''
Global configuration for Nixpkgs.
The complete list of [Nixpkgs configuration options] is in the [Nixpkgs manual section on global configuration].
Ignored when {option}`nixpkgs.pkgs` is set.
[Nixpkgs configuration options]: https://nixos.org/manual/nixpkgs/unstable/#sec-config-options-reference
[Nixpkgs manual section on global configuration]: https://nixos.org/manual/nixpkgs/unstable/#chap-packageconfig
'';
};
overlays = lib.mkOption { overlays = lib.mkOption {
type = type =
let let
@ -170,5 +216,21 @@ in
warnings = lib.optional ( warnings = lib.optional (
opt.source.isDefined && opt.source.highestPrio < (lib.mkOptionDefault null).priority opt.source.isDefined && opt.source.highestPrio < (lib.mkOptionDefault null).priority
) "Defining the option `nixpkgs.source` currently has no effect"; ) "Defining the option `nixpkgs.source` currently has no effect";
assertions = [
{
assertion = opt.pkgs.isDefined -> cfg.config == { };
message = ''
Your system configures nixpkgs with an externally created instance.
`nixpkgs.config` options should be passed when creating the instance instead.
Current value:
${lib.generators.toPretty { multiline = true; } cfg.config}
Defined in:
${lib.concatMapStringsSep "\n" (file: " - ${file}") opt.config.files}
'';
}
];
}; };
} }