Allow unfree in flakes

Hello I’m very new to flakes and nix in general. I’m trying to setup a dev env with mongodb installed using flakes. This is my flake.nix

{
  description = "A very basic flake";

  inputs = { nixpkgs.url = "github:NixOS/nixpkgs/nixos-unstable"; };

  outputs = { self, nixpkgs }:
    let pkgs = nixpkgs.legacyPackages.x86_64-linux;
    in {
      devShells.x86_64-linux.default =
        pkgs.mkShell { buildInputs = [ pkgs.mongodb ]; };
    };
}

When I run nix develop I got an error saying I need to whitelist mongodb because it’s an unfree package. Using the env variable NIXPKGS_ALLOW_UNFREE=1 does work but I was wondering how to make it work in the flake.nix file itself.

I tried some of the suggestions of the nix develop error but I couldn’t figure it out.

nix develop output
error:
       … while calling the 'derivationStrict' builtin

         at //builtin/derivation.nix:9:12: (source not available)

       … while evaluating derivation 'nix-shell'
         whose name attribute is located at /nix/store/xpnpp1sq0gmzf72r0iy2ky6r4h4i6vvb-source/pkgs/stdenv/generic/make-derivation.nix:303:7

       … while evaluating attribute 'buildInputs' of derivation 'nix-shell'

         at /nix/store/xpnpp1sq0gmzf72r0iy2ky6r4h4i6vvb-source/pkgs/stdenv/generic/make-derivation.nix:350:7:

          349|       depsHostHost                = lib.elemAt (lib.elemAt dependencies 1) 0;
          350|       buildInputs                 = lib.elemAt (lib.elemAt dependencies 1) 1;
             |       ^
          351|       depsTargetTarget            = lib.elemAt (lib.elemAt dependencies 2) 0;

       error: Package ‘mongodb-6.0.6’ in /nix/store/xpnpp1sq0gmzf72r0iy2ky6r4h4i6vvb-source/pkgs/servers/nosql/mongodb/mongodb.nix:186 has an unfree license (‘SSPL’), refusing to evaluate.

       a) To temporarily allow unfree packages, you can use an environment variable
          for a single invocation of the nix tools.

            $ export NIXPKGS_ALLOW_UNFREE=1

        Note: For `nix shell`, `nix build`, `nix develop` or any other Nix 2.4+
        (Flake) command, `--impure` must be passed in order to read this
        environment variable.

       b) For `nixos-rebuild` you can set
         { nixpkgs.config.allowUnfree = true; }
       in configuration.nix to override this.

       Alternatively you can configure a predicate to allow specific packages:
         { nixpkgs.config.allowUnfreePredicate = pkg: builtins.elem (lib.getName pkg) [
             "mongodb"
           ];
         }

       c) For `nix-env`, `nix-build`, `nix-shell` or any other Nix command you can add
         { allowUnfree = true; }
       to ~/.config/nixpkgs/config.nix.

2 Likes
-     let pkgs = nixpkgs.legacyPackages.x86_64-linux;
+     let pkgs = import nixpkgs { system = "x86_64-linux"; config.allowUnfree = true; };
9 Likes

Thanks @figsoda, that works.

Aside: what would have been a good resource to learn this by myself?

It’s not super well documented, but you can use the nixpkgs.config NixOS options by importing nixpkgs using the same config

       b) For `nixos-rebuild` you can set
         { nixpkgs.config.allowUnfree = true; }
       in configuration.nix to override this.

       Alternatively you can configure a predicate to allow specific packages:
         { nixpkgs.config.allowUnfreePredicate = pkg: builtins.elem (lib.getName pkg) [
             "mongodb"
           ];
         }
1 Like

I created this StackOverflow question to help everyone figure this out for the various different scenarios of using Nix:

Since NixOS Discourse comments are structured more like a forum of endless posts sorted by time instead of a proper question & answer site.

1 Like