Confusion regarding how to use derivations inside a NixOS config + allowUnfree derivation issue

Hi friends.
I am attempting to package a custom cursor set and reference it inside my own NixOS flakes configuration. I am at a bit of a loss and I’m not sure what the root of the issue is, so this is a bit of a long post. For reference, here is the file structure of files I will mention here.

nixos-config
├──pkgs
│  ├──phinger-cursors-gruvbox-material
│  │  └──default.nix
│  └──default.nix
└──flake.nix

So what I’ve done so far is create the derivation as “phinger-cursors-gruvbox-material/default.nix” . I don’t think this is part of the issue as it builds without error using nix build .\#phinger-cursors-gruvbox-material, but I’m providing a link to the full file just in case.

Then I import nixpkgs and use pkgs.callPackage in “pkgs/default.nix”:

{ pkgs ? import <nixpkgs> { config = { allowUnfree = true; }; } }: rec 
{
  phinger-cursors-gruvbox-material = pkgs.callPackage ./phinger-cursors-gruvbox-material { };
}

And finally I have “pkgs/default.nix” imported into the packages output in flake.nix

{
  inputs = {
    nixpkgs.url = "github:NixOS/nixpkgs/release-24.05";
  };
  outputs = { self, nixpkgs, home-manager, plasma-manager, stylix, ... } @ inputs:
  let
    system = "x86_64-linux";
  in
  {
    packages.${system} = (
      let pkgs = nixpkgs.legacyPackages.${system};
      in import ./pkgs { inherit pkgs; }
    );
  };
}

(This is an excerpt, full flake.nix is here)

So the problem is that when I try to reference pkgs.phingers-cursors-gruvbox-material somewhere in the configuration, I get this error from nix flake check:

       error: attribute 'phinger-cursors-gruvbox-material' missing

       at /nix/store/ngzg67g2hyn6n5jkayh7nghxxiv66kgr-source/hosts/common/optional/stylix.nix:30:17:

           29|     cursor = {
           30|       package = pkgs.phinger-cursors-gruvbox-material;
             |                 ^
           31|       name = "Phinger Cursors Gruvbox Material";

I would assume that after adding “pkgs/default.nix” as an output, I would be able to use the package made by the derivation anywhere just like any other package from nixpkgs. Am I missing a step somewhere? Do I need to provide more information? I would love to hear some input from someone else about this. :slight_smile:

Also according to this post. I can add config = { allowUnfree = true; }; to the imported nixpkgs and have nix stop complaining about licenses, but I’m still having that issue even after adding it.

Thanks.

You have to get your self flake input into your modules, using either approach from my blogpost below.

https://blog.nobbz.dev/2022-12-12-getting-inputs-to-modules-in-a-flake/

The prefered way to go would probably be to use specialArgs for your entrypoint, or “function taking inputs” for a module you want to expose as an output of your flake.

3 Likes

Concerning nix complaining about the unfree licence, I used the following:

# Override the Nix package set to allow unfree packages
pkgs = import nixpkgs {
  system = system; 
  config.allowUnfree = true; 
};

Have a look. Don’t forget that in system = system;, the right one is a variable in my flake (from flake-utils).

1 Like

I do not see any error related to unfree licenses. I only see a link to a thread discussing the problems that might happen with them.

The error shown here seems to be totally unrelated to unfree licenses, and if those really become a problem, then we have to tackle them in an extra step.

This is simply about not using the declared package at all, but instead trying to use an inexting attribute from pkgs, a quite common mistake when starting to have “custom packages” in a NixOS config, and this error is made regardless of flakes.

1 Like

I got it to work, but just to clarify, do you mean I should replace the pkgs that comes before the name of my package with inputs.self.packages.${pkgs.system}.?

Sorry, I don’t think I was thinking entirely straight when I added that link. I’m getting a different error than the one described in that post:

Error
       error: Package ‘phinger-cursors-gruvbox-material-3328966123’ in /nix/store/z1d7pf61x9m26z83vwmqm4i7f90k1dpp-source/pkgs/phinger-cursors-gruvbox-material/default.nix:22 has an unfree license (‘cc-by-nc-40’), 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: When using `nix shell`, `nix build`, `nix develop`, etc with a flake,
                then pass `--impure` in order to allow use of environment variables.

       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) [
             "phinger-cursors-gruvbox-material"
           ];
         }

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

I think I meant to say that I already do have { nixpkgs.config.allowUnfree = true; } set in my “pkgs/default.nix” file. I hope this makes more sense.

At the moment you have this error, it is important to know exactly how you use that package how you define it, and how you pass it around.

If i’m understanding you correctly:
I’m trying to use the sudo nixos-rebuild switch command.
I’m using the package in the stylix.cursor.package option from Stylix.
The package is defined as:

phinger-cursors-gruvbox-material/default.nix

{ lib, stdenvNoCC, fetchurl }:

stdenvNoCC.mkDerivation rec {
pname = “phinger-cursors-gruvbox-material”;
version = “3328966123”;

src = fetchurl {
url = “https://github.com/rehanzo/phinger-cursors-gruvbox-material/releases/download/${version}/phinger-cursors-variants.tar.bz2”;
hash = “sha256-qAEGY3B0tphEwYGfhkJ555yLgAu1nflCjqCOfZ8vjIE=”;
};

sourceRoot = “.”;

installPhase = ‘’
runHook preInstall
mkdir -p $out/share/icons
cp -r ./phinger-cursors-gruvbox-material* $out/share/icons
runHook postInstall
‘’;

meta = with lib; {
description = “The Phinger cursor theme, recolored to fit the Gruvbox Material color scheme”;
homepage = “GitHub - rehanzo/phinger-cursors-gruvbox-material: Most likely the most over engineered cursor theme.”;
platform = platforms.unix;
license = licenses.cc-by-nc-40;
maintainers = with maintainers; [ Fireb3rt ];
};
}

And the package is being passed from my flake.nix outputs to a module called stylix.nix. Which looks like this:

hosts/common/optional/stylix.nix
{ inputs, pkgs, ... }: {
  stylix = {
    enable = true;

    image = pkgs.fetchurl {
      url = "https://i.imgur.com/GF5bOT9.png";
      sha256 = "8805fa5b4392eb879d3bbca60600fc4dfebc8cefda21309343f2c9cef8c81ff0";
    };
    base16Scheme = "${pkgs.base16-schemes}/share/themes/gruvbox-material-dark-medium.yaml";
    polarity = "dark";

    fonts = {
      serif = {
        package = pkgs.roboto;
        name = "Roboto";
      };

      sansSerif = {
        package = pkgs.roboto;
        name = "Roboto";
      };

      monospace = {
        package = (pkgs.nerdfonts.override { fonts = [ "0xProto" ]; });
        name = "0xProto Nerd Font";
      };
    }; 

    cursor = {
      package = inputs.self.packages.${pkgs.system}.phinger-cursors-gruvbox-material;
      name = "Phinger Cursors Gruvbox Material";
      size = 32;
    };
  };
}

How exactly is your output defined? You will probably have to configure the nixpkgs correctly there.

I’m sorry, I’m not sure what you mean. Is one of these what you’re referring to?

This one. You have to import and configure nixpkgs here to allow unfree.

2 Likes