Create and add a file to a flake package

Hello

I am building a NixOSConfiguration and a package with initrd and the kernel for future use.

{
  description = "A very basic flake";
  inputs = {
    nixpkgs.url = "github:NixOS/nixpkgs/nixos-22.05";
    deploy-rs.url = "github:serokell/deploy-rs";
  };
  outputs = { self, nixpkgs, deploy-rs }: {

    nixosConfigurations = {
      blackhole = nixpkgs.lib.nixosSystem {
        system = "x86_64-linux";
        modules = [
          ({ config, modulesPath, ... }: {
            imports = [
              (modulesPath + "/installer/netboot/netboot-base.nix")
            ];
          })
        ];
      };
    };

    packages.x86_64-linux.blackhole = nixpkgs.legacyPackages.x86_64-linux.symlinkJoin {
      name = "blackhole";
      paths = with self.nixosConfigurations.blackhole.config.system.build; [
        netbootRamdisk
        kernel
      ];
    };
  };
}

I build it with:

$ nix build .#blackhole

The netboot module can generate a file called netbooting.pxe nixpkgs/netboot.nix at 9bc01704b4c2ac26d90ebe83accd5c0833afbbd3 · NixOS/nixpkgs · GitHub

I would like to add to my package a similar file containing only the path of the init script: init=${config.system.build.toplevel}/init because I would like to read that file later when I use this package.

Any feedback about how I can do it?
Thanks a lot

1 Like

I’ve not tested, but my natural guess would be to simply create a derivation like:

let myInit = runCommand "init" {} ''
  mkdir -p $out/bin/
  cp ${config.system.build.toplevel}/init $out/bin
''; in …

and add it in you paths:

…
      paths = with self.nixosConfigurations.blackhole.config.system.build; [
        netbootRamdisk
        kernel
        myInit
      ];
…

Have you tried that by any chance?

I was able to apply your suggestion as follow:

{
  description = "A very basic flake";
  inputs = {
    nixpkgs.url = "github:NixOS/nixpkgs/nixos-22.05";
  };
  outputs = { self, nixpkgs }: {

    nixosConfigurations = {
      blackhole =
        nixpkgs.lib.nixosSystem {
          system = "x86_64-linux";
          modules = [
            (

              { config, pkgs, lib, modulesPath, ... }: with lib;
              {
                imports = [
                  (modulesPath + "/installer/netboot/netboot-base.nix")
                ];
                system.build.myInit = pkgs.runCommand "init" { } ''
                  mkdir -p $out
                  echo "init=${config.system.build.toplevel}/init initrd=initrd loglevel=4" > $out/init
                '';
                system.stateVersion = "22.05";
              }

            )
          ];
        };
    };

    packages.x86_64-linux.blackhole = nixpkgs.legacyPackages.x86_64-linux.symlinkJoin {
      name = "blackhole";
      paths = with self.nixosConfigurations.blackhole.config.system.build; [
        netbootRamdisk
        kernel
        netbootIpxeScript
        myInit
      ];
    };
  };
}

And it works great! Thanks a lot

$ nix build .#blackhole
$ cat result/init
init=/nix/store/pw4yn8d9p08adj89dx7yzccwbjiwcyk8-nixos-system-nixos-22.05.20221007.9ecc270/init initrd=initrd loglevel=4
1 Like