Using unfree packages from flakes

I have written a flake to provide a NixOS module for running a non-free game server, and I have had to comment out the meta.license definition, because I could not figure out how to tell nix that it’s OK to install these unfree packages. I have run into a similar situation with trying to use unfree packages from nixpkgs within a flake, and the solution was to set nixpkgs.config.allowUnfree = true; inside the flake. But that is having no effect here, where the package is coming from within the flake, not from nixpkgs.

This is the flake in question, and the “Usage” section of the README shows how I have it plumbed into a NixOS system.

You should be using a different design for the nixOS module. First the output schema for nixosModules is not qualified by the system.

Second, the module should not be using the packages output from the flake. Instead, the packages should come from using callPackage on the pkgs input to the module. That way it respects any overlays and configuration (including the allowNonFree option) from the containing nixOS configuration. One way to approach this is to define an overlay for the packages in the flake and also use this overlay for the nixpkgs.overlays option in the module.

With that change, there is no longer any need to add the valheim-server-flake attribute as a special arg. Even then, if there was need to reference the flake this is not my preferred way to do it. I would instead make the module file be a function with two arguments: self-flake: { config, pkgs, lib, ... }: /* ... */ and then when defining the nixosModules output use { valheim = import ./nixos-modules/valheim.nix self; /* ... */ } so that within the module self-flake refers to the valheim server flake.

1 Like

Thanks, that makes much more sense, and gets rid of the circular logic with passing the flake into the module. But I seem to have made a mistake somewhere converting my flakes to the overlay pattern. Using the overlay-pattern branch of the valheim-server-flake, and following the NixOS config pattern outlined in the README on that branch, I get the following error when I run nixos-rebuild.

building the system configuration...
error: attribute 'valheim-server-unwrapped' missing

       at /nix/store/jdiad8mpsjl0g09nirglw2jx2ssiscld-source/nixos-modules/valheim.nix:107:15:

          106|           cp -r \
          107|             ${pkgs.valheim-server-unwrapped}/* \
             |               ^
          108|             ${pkgs.valheim-plus}/* \

This is very close. In your readme file you construct a package set that uses the overlay when you write let pkgs = import nixpkgs { overlays = [ ... ]; ... }; but then that package set is not used anywhere. This is why the valheim packages are not found in the resulting configuration.

It seems I wasn’t exactly clear. I’ll try to be a little more explicit. What I mean is that within nixos-modules/valheim.nix you can add nixpkgs.overlays = [ /* valheim overlay */ ]; to the config. Now, how should we get what replaces /* valheim overlay */?

One option is to extract the overlay definition to its own nix file and use something like import ./overlay.nix in both flake.nix (for the overlays.default output) and in nixos-modules/valheim.nix (for the nixpkgs.overlays option but here it would be something like import ./../overlay.nix).

Another option is to add an initial argument to the nixos-modules/valheim.nix file to pass a reference to the self flake into the module as I mentioned above. Then you can get the overlay as self-flake.overlays.default.

Now, with that all said, It also seems that you will need the fetchSteam overlay provided by the steam-fetcher input for the nixos module to work so I would go with the second option, but now passing in both self and steam-fetcher in an attrset so that in flake.nix you’d have something like

nixosModules.default = import ./nixos-modules/valheim.nix { self, steam-fetcher };

and then in nixos-modules/valheim.nix you’d have

{ self, steam-fetcher }: { config, pkgs, lib, ... }:
{
  ...
  nixpkgs.overlays = [ self.overlays.default steam-fetcher.overlays.default ];
  ...
};
1 Like

That was it! This looks much tidier now. Thank you so much for your help!