From a3c9221d642da4c0b22b9c9576bd2af8dcbb8263 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E2=80=AErekc=C3=A4H=20nitraM=E2=80=AE?= Date: Sun, 6 Apr 2025 18:36:51 +0200 Subject: [PATCH] nixos/misc/nixpkgs: add setting nixpkgs.config.allowUnfreePackages = ["list" "of "packages"] Inspired by https://github.com/NixOS/nixpkgs/issues/197325 this adds a new option nixpkgs.allowUnfreePackages, which merges additively and can thus be defined in multiple modules close to where the unfree package is installed. I would have liked ot name this option nixpkgs.config.allowUnfreePackages, to define it closer to where the allowUnfree and allowUnfreePredicate are defined, but I didn't see how this could be achived. I would welcome some guidance on how to do this. --- nixos/modules/misc/nixpkgs.nix | 3 +++ pkgs/stdenv/generic/check-meta.nix | 5 ++++- pkgs/top-level/config.nix | 17 +++++++++++++++++ 3 files changed, 24 insertions(+), 1 deletion(-) diff --git a/nixos/modules/misc/nixpkgs.nix b/nixos/modules/misc/nixpkgs.nix index 449a08a97b61..e832cfd2ebdd 100644 --- a/nixos/modules/misc/nixpkgs.nix +++ b/nixos/modules/misc/nixpkgs.nix @@ -20,6 +20,9 @@ let rhs = optCall rhs_ { inherit pkgs; }; in lib.recursiveUpdate lhs rhs + // lib.optionalAttrs (lhs ? allowUnfreePackages) { + allowUnfreePackages = lhs.allowUnfreePackages ++ (lib.attrByPath [ "allowUnfreePackages" ] [ ] rhs); + } // lib.optionalAttrs (lhs ? packageOverrides) { packageOverrides = pkgs: diff --git a/pkgs/stdenv/generic/check-meta.nix b/pkgs/stdenv/generic/check-meta.nix index 28de9c68110e..688429685863 100644 --- a/pkgs/stdenv/generic/check-meta.nix +++ b/pkgs/stdenv/generic/check-meta.nix @@ -158,7 +158,10 @@ let # allowUnfree = false; # allowUnfreePredicate = (x: pkgs.lib.hasPrefix "vscode" x.name); # } - allowUnfreePredicate = config.allowUnfreePredicate or (x: false); + # Defaults to allow all names defined in config.allowUnfreePackages + allowUnfreePredicate = + config.allowUnfreePredicate + or (pkg: builtins.elem (lib.getName pkg) config.allowUnfreePackages or [ ]); # Check whether unfree packages are allowed and if not, whether the # package has an unfree license and is not explicitly allowed by the diff --git a/pkgs/top-level/config.nix b/pkgs/top-level/config.nix index c8e8a3f58590..9d8f1e442c5a 100644 --- a/pkgs/top-level/config.nix +++ b/pkgs/top-level/config.nix @@ -226,6 +226,23 @@ let ''; }; + allowUnfreePackages = mkOption { + type = with lib.types; listOf str; + default = [ ]; + example = [ "ut1999" ]; + description = '' + Allows specific unfree packages to be used. + + This option composes with `nixpkgs.config.allowUnfreePredicate` by also allowing the listed package names. + + Unlike `nixpkgs.config.allowUnfreePredicate`, this option merges additively, similar to `environment.systemPackages`. + This enables defining allowed unfree packages in multiple modules, close to where they are used. + + This avoids the need to centralize all unfree package declarations or globally enable unfree packages via + `nixpkgs.config.allowUnfree = true`. + ''; + }; + allowBroken = mkOption { type = types.bool; default = false;