# Using non-free libraries in flakes

**URL:** https://discourse.nixos.org/t/using-non-free-libraries-in-flakes/8632
**Category:** Help
**Created:** [August 18, 2020, 6:18pm UTC](https://discourse.nixos.org/t/using-non-free-libraries-in-flakes/8632 "2020-08-18T18:18:25Z")
**Posts on this page:** 19
**Page:** 1

<div class="post-metadata">

### Author: ![swflint](https://discourse.nixos.org/user_avatar/discourse.nixos.org/swflint/32/998_2.png) [@swflint](https://discourse.nixos.org/u/swflint)
#### Post date: [August 18, 2020, 6:18pm UTC](https://discourse.nixos.org/t/using-non-free-libraries-in-flakes/8632/1 "2020-08-18T18:18:25Z")

</div>

Hello!

I’m trying to use nix flakes to package some development code and  
provide the relevant development environment. However, one of the  
python libraries I need uses mkl, and `nix develop` refuses to fully  
evaluate because of this (as it’s unfree).

What then, should I do to enable the use of this unfree library in a  
flake?

Thanks,

Sam

---

<div class="post-metadata">

### Author: ![jonringer](https://discourse.nixos.org/user_avatar/discourse.nixos.org/jonringer/32/1348_2.png) [@jonringer](https://discourse.nixos.org/u/jonringer)
#### Post date: [August 18, 2020, 6:46pm UTC](https://discourse.nixos.org/t/using-non-free-libraries-in-flakes/8632/2 "2020-08-18T18:46:31Z")

</div>

This should be the same for most nix packages usage:

```auto
$ cat ~/.config/nixpkgs/config.nix
{
  allowUnfree = true;
}

```

---

<div class="post-metadata">

### Author: ![swflint](https://discourse.nixos.org/user_avatar/discourse.nixos.org/swflint/32/998_2.png) [@swflint](https://discourse.nixos.org/u/swflint)
#### Post date: [August 18, 2020, 7:02pm UTC](https://discourse.nixos.org/t/using-non-free-libraries-in-flakes/8632/3 "2020-08-18T19:02:23Z")

</div>

It’s set to true there. Error is as follows:

```
error: --- ThrownError -------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------- nix
Package ‘mkl-2020.1.217’ in /nix/store/2gvpjwk27dng6c6vw3q3dpfdbza91wmn-source/pkgs/development/libraries/science/math/mkl/default.nix:151 has an unfree license (‘issl’), refusing to evaluate.

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

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

(use '--show-trace' to show detailed location information)

```

nix-shell with flake-compatibility works, however.

Thanks,

Sam

---

<div class="post-metadata">

### Author: ![jonringer](https://discourse.nixos.org/user_avatar/discourse.nixos.org/jonringer/32/1348_2.png) [@jonringer](https://discourse.nixos.org/u/jonringer)
#### Post date: [August 18, 2020, 7:33pm UTC](https://discourse.nixos.org/t/using-non-free-libraries-in-flakes/8632/4 "2020-08-18T19:33:28Z")

</div>

do you mind pasting the command that’s failing?

you mention `nix develop`, but that’s not an official nix command (at least on 2.3.7)

EDIT: I guess `nix develop` is only available on the `nixFlakes` verions of nix cli.

---

<div class="post-metadata">

### Author: ![zimbatm](https://discourse.nixos.org/user_avatar/discourse.nixos.org/zimbatm/32/4_2.png) [@zimbatm](https://discourse.nixos.org/u/zimbatm)
#### Post date: [August 18, 2020, 7:54pm UTC](https://discourse.nixos.org/t/using-non-free-libraries-in-flakes/8632/5 "2020-08-18T19:54:52Z")

</div>

Flakes evaluations are pure and therefor don’t source configuration from the user’s home.

The issue is that when using `nixpkgs.legacyPackages.x86_64-linux.mkl`, that instance of nixpkgs has already been configured. Instead, try importing and constructing another instance of nixpkgs with `allowUnfree = true;` in the config.

Before:

```nix
{
  description = "Flake utils demo";

  inputs.flake-utils.url = "github:numtide/flake-utils";

  outputs = { self, nixpkgs, flake-utils }:
    flake-utils.lib.eachDefaultSystem (system:
      let pkgs = nixpkgs.legacyPackages.${system}; in
      {
        defaultPackage = pkgs.mkl;
      }
    );
}

```

After:

```nix
{
  description = "Flake utils demo";

  inputs.flake-utils.url = "github:numtide/flake-utils";

  outputs = { self, nixpkgs, flake-utils }:
    flake-utils.lib.eachDefaultSystem (system:
      let
        pkgs = import nixpkgs {
          config = { allowUnfree = true; };
        };
      in 
      {
        defaultPackage = pkgs.mkl;
      }
    );
}

```

---

<div class="post-metadata">

### Author: ![jonringer](https://discourse.nixos.org/user_avatar/discourse.nixos.org/jonringer/32/1348_2.png) [@jonringer](https://discourse.nixos.org/u/jonringer)
#### Post date: [August 18, 2020, 8:06pm UTC](https://discourse.nixos.org/t/using-non-free-libraries-in-flakes/8632/6 "2020-08-18T20:06:29Z")

</div>

> [@zimbatm](#):
>
> Flakes evaluations are pure

So does this also mean that flakes do not take into account local overlays, and really anything else mentioned `pkgs/top-level/impure.nix`?

---

<div class="post-metadata">

### Author: ![zimbatm](https://discourse.nixos.org/user_avatar/discourse.nixos.org/zimbatm/32/4_2.png) [@zimbatm](https://discourse.nixos.org/u/zimbatm)
#### Post date: [August 18, 2020, 8:25pm UTC](https://discourse.nixos.org/t/using-non-free-libraries-in-flakes/8632/7 "2020-08-18T20:25:45Z")

</div>

Actually, I haven’t checked what it does with paths starting with `~/`. But it’s not in the spirit of Flakes if it works.

One of the goals is to allow caching of evaluations, which requires to turn off sources of impurities. So builtins like `builtins.currentTime`, `builtins.currentSystem`, `builtins.getEnv` are also disabled or return default values.

---

<div class="post-metadata">

### Author: ![swflint](https://discourse.nixos.org/user_avatar/discourse.nixos.org/swflint/32/998_2.png) [@swflint](https://discourse.nixos.org/u/swflint)
#### Post date: [August 18, 2020, 8:35pm UTC](https://discourse.nixos.org/t/using-non-free-libraries-in-flakes/8632/8 "2020-08-18T20:35:22Z")

</div>

This works, mostly. If I want to specify a specific version of nixpkgs  
with

```
inputs.nixpkgs.url = "github:NixOS/nixpkgs/nixos-20.03";

```

I get an error about attribute currentSystem being missing:

```
warning: updating lock file '.../flake.lock':
* Updated 'nixpkgs': 'github:NixOS/nixpkgs/cb1996818edf506c0d1665ab147c253c558a7426' -> 'github:NixOS/nixpkgs/feb9c28902eca0384484004e0b7dfe5ac8661d6c'
warning: Git tree '...' is dirty
error: --- EvalError ---------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------- nix
at: (90:39) in file: /nix/store/hbqx9yg7zc2w70l3myhgrsyhblib5bxm-source/pkgs/top-level/impure.nix

    89| else (if args ? localSystem then {}
    90| else { system = builtins.currentSystem; }) // localSystem;
      > ^
    91| })

attribute 'currentSystem' missing
(use '--show-trace' to show detailed location information)

```

Thanks,

Sam

---

<div class="post-metadata">

### Author: ![jonringer](https://discourse.nixos.org/user_avatar/discourse.nixos.org/jonringer/32/1348_2.png) [@jonringer](https://discourse.nixos.org/u/jonringer)
#### Post date: [August 18, 2020, 8:54pm UTC](https://discourse.nixos.org/t/using-non-free-libraries-in-flakes/8632/9 "2020-08-18T20:54:05Z")

</div>

As @zimbatm mentioned

> One of the goals is to allow caching of evaluations, which requires to turn off sources of impurities. So builtins like `builtins.currentTime` , `builtins.currentSystem` , `builtins.getEnv` are also disabled or return default values.

you’ll want to replace it with your actual system

```auto
nix-repl> lib.platforms.all
["aarch64-linux" "armv5tel-linux" "armv6l-linux" "armv7a-linux" "armv7l-linux" "mipsel-linux" "i686-cygwin" "i686-freebsd" "i686-linux" "i686-netbsd" "i686-openbsd" "x86_64-cygwin" "x86_64-freebsd" "x86_64-linux" "x86_64-netbsd" "x86_64-openbsd" "x86_64-solaris" "x86_64-darwin" "i686-darwin" "aarch64-darwin" "armv7a-darwin" "x86_64-windows" "i686-windows" "wasm64-wasi" "wasm32-wasi" "x86_64-redox" "powerpc64le-linux" "riscv32-linux" "riscv64-linux" "arm-none" "armv6l-none" "aarch64-none" "avr-none" "i686-none" "x86_64-none" "powerpc-none" "msp430-none" "riscv64-none" "riscv32-none" "vc4-none" "js-ghcjs" "aarch64-genode" "i686-genode" "x86_64-genode"]

```

in your case, it’s likely `x86_64-linux` assuming you’re on a 64bit linux machine

---

<div class="post-metadata">

### Author: ![ryantm](https://discourse.nixos.org/user_avatar/discourse.nixos.org/ryantm/32/900_2.png) [@ryantm](https://discourse.nixos.org/u/ryantm)
#### Post date: [August 19, 2020, 1:24pm UTC](https://discourse.nixos.org/t/using-non-free-libraries-in-flakes/8632/10 "2020-08-19T13:24:33Z")

</div>

Like this assuming you only care about one system type: [https://github.com/ryantm/nixpkgs-update/blob/5749f0029adbf4599b27f5b60a4d7d3d9b88c28d/flake.nix#L20](https://github.com/ryantm/nixpkgs-update/blob/5749f0029adbf4599b27f5b60a4d7d3d9b88c28d/flake.nix#L20)

---

<div class="post-metadata">

### Author: ![swflint](https://discourse.nixos.org/user_avatar/discourse.nixos.org/swflint/32/998_2.png) [@swflint](https://discourse.nixos.org/u/swflint)
#### Post date: [August 19, 2020, 3:14pm UTC](https://discourse.nixos.org/t/using-non-free-libraries-in-flakes/8632/11 "2020-08-19T15:14:11Z")

</div>

I potentially care about more than one system type. I had been using  
flake-utils and eachDefaultSystem. I ended up adding `inherit system;`  
in the `import nixpkgs...` and it works.

Thanks,

Sam

---

<div class="post-metadata">

### Author: ![terlar](https://discourse.nixos.org/user_avatar/discourse.nixos.org/terlar/32/868_2.png) [@terlar](https://discourse.nixos.org/u/terlar)
#### Post date: [December 9, 2020, 2:16pm UTC](https://discourse.nixos.org/t/using-non-free-libraries-in-flakes/8632/12 "2020-12-09T14:16:20Z")

</div>

Anyone has any idea how to solve this for the nix run and nix shell command?

For example:

```auto
nix run nixpkgs/release-20.09#non-free-software
nix shell nixpkgs/release-20.09#non-free-software

```

---

<div class="post-metadata">

### Author: ![NobbZ](https://discourse.nixos.org/user_avatar/discourse.nixos.org/nobbz/32/7022_2.png) [@NobbZ](https://discourse.nixos.org/u/NobbZ)
#### Post date: [December 9, 2020, 2:28pm UTC](https://discourse.nixos.org/t/using-non-free-libraries-in-flakes/8632/13 "2020-12-09T14:28:51Z")

</div>

I tend to use `NIXPKGS_ALLOW_UNFREE=1 nix {run,shell} --impure nixpkgs/release-20.09#package` for one offs.

---

<div class="post-metadata">

### Author: ![terlar](https://discourse.nixos.org/user_avatar/discourse.nixos.org/terlar/32/868_2.png) [@terlar](https://discourse.nixos.org/u/terlar)
#### Post date: [December 9, 2020, 2:36pm UTC](https://discourse.nixos.org/t/using-non-free-libraries-in-flakes/8632/14 "2020-12-09T14:36:56Z")

</div>

Thank you, I wasn’t aware it supported impure, that solves my problem for one-offs.

---

<div class="post-metadata">

### Author: ![doronbehar](https://discourse.nixos.org/user_avatar/discourse.nixos.org/doronbehar/32/1387_2.png) [@doronbehar](https://discourse.nixos.org/u/doronbehar)
#### Post date: [December 9, 2020, 3:08pm UTC](https://discourse.nixos.org/t/using-non-free-libraries-in-flakes/8632/15 "2020-12-09T15:08:14Z")

</div>

Related: [check-meta.nix: Fix message for `nix build` users as well by doronbehar · Pull Request #101367 · NixOS/nixpkgs · GitHub](https://github.com/NixOS/nixpkgs/pull/101367) .

---

<div class="post-metadata">

### Author: ![teto](https://discourse.nixos.org/user_avatar/discourse.nixos.org/teto/32/182_2.png) [@teto](https://discourse.nixos.org/u/teto)
#### Post date: [December 9, 2020, 9:46pm UTC](https://discourse.nixos.org/t/using-non-free-libraries-in-flakes/8632/16 "2020-12-09T21:46:54Z")

</div>

Not something I am proud of but I prefer it to either using --impure or importing the pkgs with a proper config, I forked nixpkgs with `allowUnfree` in check-meta.nix that always returns true:

```auto
  allowUnfree = config.allowUnfree or true
    || builtins.getEnv "NIXPKGS_ALLOW_UNFREE" == "1";

```

Despise me

---

<div class="post-metadata">

### Author: ![aevans](https://discourse.nixos.org/letter_avatar_proxy/v4/letter/a/839c29/32.png) [@aevans](https://discourse.nixos.org/u/aevans)
#### Post date: [August 7, 2021, 4:36am UTC](https://discourse.nixos.org/t/using-non-free-libraries-in-flakes/8632/18 "2021-08-07T04:36:26Z")

</div>

I found this post after hitting the same problem and searching on Google. If anyone else finds this in the future the below works for me. Note the `inherit system` in the nixpkgs argument

```auto
{
  inputs = {
    nixpkgs.url = "github:nixos/nixpkgs";
    flake-utils.url = "github:numtide/flake-utils";
  };

  outputs = { self, flake-utils, nixpkgs}:
    flake-utils.lib.eachDefaultSystem (system:
      let
        pkgs = import nixpkgs {
          inherit system;
          config = { allowUnfree = true; };
        };
      in {
        devShell = pkgs.mkShell {
          buildInputs = with pkgs;[
            wrangler
            cloudflared
          ];
        };
      }
    );
}

```

---

<div class="post-metadata">

### Author: ![sehe](https://discourse.nixos.org/user_avatar/discourse.nixos.org/sehe/32/16302_2.png) [@sehe](https://discourse.nixos.org/u/sehe)
#### Post date: [March 24, 2024, 3:55pm UTC](https://discourse.nixos.org/t/using-non-free-libraries-in-flakes/8632/19 "2024-03-24T15:55:49Z")

</div>

Wow. That finally helped me.

That’s completely non-obvious to me, as the original spelling had `.legacyPackages.${system}`, so I was trying all manner of syntax where I crammed the `.legacyPackages` in as well. Also, `import nixpkgs { system = ${system}; ...` which apparently is not equivalent.

All this is Very Hard ™ to figure out without a full understanding of the Nix language and all the various relations between NixOs, flakes, home-manager etc.

Just for anyone else who like a visual demonstration: **before**

```nix
flake-utils.lib.eachDefaultSystem (system: let
  pkgs = nixpkgs.legacyPackages.${system};

```

**after**

```nix
flake-utils.lib.eachDefaultSystem (system: let
  pkgs = import nixpkgs { inherit system; config.allowUnfree = true; };

```

---

<div class="post-metadata">

### Author: ![toastal](https://discourse.nixos.org/user_avatar/discourse.nixos.org/toastal/32/3454_2.png) [@toastal](https://discourse.nixos.org/u/toastal)
#### Post date: [April 26, 2024, 11:19am UTC](https://discourse.nixos.org/t/using-non-free-libraries-in-flakes/8632/20 "2024-04-26T11:19:27Z")

</div>

You may want to consider using `allowlistedLicenses` or `allowUnfreePredicate` instead of `allowUnfree` so it’s not a blanket statement for easier auditing. You also probably do not need `flake-utils` if you are using flakes as the main tool for dependency pinning.
