allowUnfreePredicate fails in my system flake

Hello,

I switched to NixOS few days ago and I have trouble figuring out how to allow only specific unfree packages. I’m using flakes to configure my system and you can find it here: GitHub - MatejaMaric/nix-setup: My Nix Flake configuration

From the docs I saw that I can use this code the enable only specific unfree packages:

{lib, ...}: {
  nixpkgs.config.allowUnfreePredicate = pkg:
    builtins.elem (lib.getName pkg) [
      # Add additional package names here
      "hello-unfree"
    ];
}

However when I add it to my configuration.nix it fails with the following error:

mateja@hp-laptop:nix-setup$ sudo nixos-rebuild switch --flake ~/nix-setup/.#                                                                                                                  
[sudo] password for mateja:                                                                                                                                                                   
building the system configuration...                                                           
error:                                                                                                                                                                                        
       … while calling the 'head' builtin                                                      
                                                                                               
         at /nix/store/jnk1mfi594y0dsmzla4nmkbr4rnrqa71-source/lib/attrsets.nix:960:11:        
                                                                                               
          959|         || pred here (elemAt values 1) (head values) then                                                                                                                      
          960|           head values                                                                                                                                                          
             |           ^                     
          961|         else                                                                    
                                                                                               
       … while evaluating the attribute 'value'                                                
                                                                                               
         at /nix/store/jnk1mfi594y0dsmzla4nmkbr4rnrqa71-source/lib/modules.nix:809:9:                                                                                                         
                                                                                               
          808|     in warnDeprecation opt //                                                                                                                                                  
          809|       { value = builtins.addErrorContext "while evaluating the option `${showOption loc}':" value;                                                                             
             |         ^                                                                       
          810|         inherit (res.defsFinal') highestPrio;                                                                                                                                  
                                                                                               
       (stack trace truncated; use '--show-trace' to show the full trace)                                                                                                                     
                                                                                               
       error:                                                                                                                                                                                 
       Failed assertions:                                                                      
       - Your system configures nixpkgs with an externally created instance.                   
       `nixpkgs.config` options should be passed when creating the instance instead.           
                                                                                            
       Current value:                                                                                                                                                                         
       {                                                                                                                                                                                      
         __toString = <function>;              
         _type = "option";                                                                     
         declarationPositions = [                                                              
           {                                                                                   
             column = 5;                                                                       
             file = "/nix/store/jnk1mfi594y0dsmzla4nmkbr4rnrqa71-source/nixos/modules/misc/nixpkgs.nix";                                                                                      
             line = 148;                                                                       
           }                                                                                                                                                                                  
         ];                                                                                                                                                                                   
         declarations = [                                                                      
           "/nix/store/jnk1mfi594y0dsmzla4nmkbr4rnrqa71-source/nixos/modules/misc/nixpkgs.nix"                                                                                                
         ];                                                                                    
         default = { };                                                                                                                                                                       
         definitions = [                                                                       
           {                                                                                                                                                                                  
             allowUnfreePredicate = <function>;                                                
           }                                                                                   
         ];                                                                                    
         definitionsWithLocations = [                                                          
         definitionsWithLocations = [                                                                                                                                               [233/1106]
           {                                                                                                                                                                                  
             file = "/nix/store/lm6asgi57y3jhnyvn7zfs4ih19fs06q4-source/hosts/hp-laptop/configuration.nix";
             value = {
               allowUnfreePredicate = <function>;
             };
           }
         ];
         description = ''
           The configuration of the Nix Packages collection.  (For
           details, see the Nixpkgs documentation.)  It allows you to set
           package configuration options.

           Ignored when `nixpkgs.pkgs` is set.
         '';
         example = {
           _type = "literalExpression";
           text = ''
             { allowBroken = true; allowUnfree = true; }
           '';
         };
         files = [
           "/nix/store/lm6asgi57y3jhnyvn7zfs4ih19fs06q4-source/hosts/hp-laptop/configuration.nix"
         ];
         highestPrio = 100;
         isDefined = true;
         loc = [
           "nixpkgs"
           "config"
         ];
         options = [ ];
         type = {
           _type = "option-type";
           check = <function>;
           deprecationMessage = null;
           description = "nixpkgs config";
           descriptionClass = null;
           emptyValue = { };
           functor = {
             binOp = <function>;
             name = "nixpkgs-config";
             payload = null;
             type = null;
             wrapped = null;
           };
           getSubModules = null;
           getSubOptions = <function>;
           merge = <function>;
           name = "nixpkgs-config";
           nestedTypes = { };
           substSubModules = <function>;
           typeMerge = <function>;
         };
         value = {
           allowUnfreePredicate = <function>;
         };
       }

1 Like

I found the solution. I had to move the allowUnfreePredicate from configuration.nix module to the configuration of nixpkgs in nixosSystem. Here’s the commit for more details if you are interested: fix(hp-laptop): moved the allowUnfreePredicate option · MatejaMaric/nix-setup@87366f7 · GitHub

2 Likes