From nixos flake config, extract custom derivation into separate flake

Hi,

I have my NixOS config in a flake. I have some custom derivations that I want to extract as independent flakes on GitHub and then use in my config. After some trial and error and searching, I did not solve it yet. This can’t be! :smiley: This should be pretty default, hence I request your kind help :pray:

My current config is as follows (with flake.nix loading the module home.nix which in turn imports the module audio.nix):

# flake.nix shortened

{
  description = "my nixos";

  inputs = ...

  outputs = { self, nixpkgs, ... }:
    let
      system = "x86_64-linux";
    in
    {
      nixosConfigurations.myhost =
        let
          bla = "blub";
        in
        nixpkgs.lib.nixosSystem {
          inherit system;
          specialArgs = { inherit bla; };
          modules = [
            ...

            ./configuration.nix

            home-manager.nixosModules.home-manager
            ./home.nix
          ];
        };
      ...
    };
  ...
}
# home.nix shortened

{ username, uid, ... }: {
  ...

  home-manager.users.${username} = {
    programs.home-manager.enable = false;

    home = {
      stateVersion = "23.05";

      inherit username;
      homeDirectory = "/home/${username}";
    };

    imports = [
      ...

      ../../home-manager/audio.nix

      ...
    ];
  };
}

and

# audio.nix shortened

{ config, pkgs, ... }:
let
  ...
in
let
  camilladsp = mkDerivation {
    name = "camilladsp";
    src = fetchzip {
      url = "https://github.com/HEnquist/camilladsp/releases/download/v1.0.3/camilladsp-linux-amd64.tar.gz";
      hash = "sha256-zWOyPmaHRi2VIRvzFpS02tPlXNn90ogU2Q/YRx7l6eI=";
    };
    phases = [
      "installPhase"
      "preFixup"
    ];
    installPhase = ''
      install -D $src/camilladsp $out/bin/camilladsp
    '';
    preFixup =
      let
        libPath = lib.makeLibraryPath [
          alsaLib
          pulseaudio
        ];
      in
      ''
        patchelf \
          --set-interpreter "$(cat $NIX_CC/nix-support/dynamic-linker)" \
          --set-rpath "${libPath}" \
          $out/bin/camilladsp
      '';
  };
in
{
  home.packages = [

    camilladsp

    ... # more custom derivations here
  ];

  ... 
}

It all works. (Please don’t mind the folder structure. I’m currently refactoring to a flake and the folder structure are WIP.) Extracting camilladsp into a package of a separate flake, my goal is to

  • add it to the inputs of my current flake (i tried already and nix flake check succeeded)
  • pass the package on to audio.nix (through home.nix)

That passing the package on is where I fail. Does anybody have a pointer or example of how to achieve this? Should I take a totally different approach?

Thanks in advance! :smiley:

I made an attempt using an overlay. It works, however I don’t know if this follows best practice. So please do not hesitate to sharpen my understanding of NixOS :wink:

I applied an overlay for pkgs already in flake.nix (other sources indicate that should be done in configuration.nix or home.nix). The overlayed pkgs I then pass as a specialArg to the lib.nixosSystem.

# flake.nix shortened

{
  description = "my nixos";

  inputs = {
    ... # from before

    # CHANGED HERE
    camilladsp = {
      url = "github:bertbesser/nix-flakes/df6381a3013b94a8ad114e6243af444a2ddea9ca?dir=camilladsp";
      inputs.nixpkgs.follows = "nixpkgs";
    };
  };

  # CHANGED HERE
  outputs = { self, nixpkgs, camilladsp, ... }:
    let
      system = "x86_64-linux";
    in
    {
      nixosConfigurations.myhost =
        let
          bla = "blub";

          # CHANGED HERE
          pkgs = nixpkgs.legacyPackages.${system}.extend (final: prev: {
            camilladsp = camilladsp.packages.${system}.default;
          });
        in
        nixpkgs.lib.nixosSystem {
          inherit system;

          # CHANGED HERE
          specialArgs = { inherit pkgs bla; };
          modules = [
            ...

            ./configuration.nix

            home-manager.nixosModules.home-manager
            ./home.nix
          ];
        };
      ...
    };
  ...
}

In audio.nix all that remained to do is remove the let camilladsp = ... in and use pkgs.camilladsp instead.

Cheers.