From a legacy NixOS configuration to Flake

Hello
I am writing a new NixOS configuration for a new project I got and I decided to move it to flake.

I had a look at this guide (Moving Nixos System Configuration Into A Flake - YouTube) but I am doing a slightly differnet thing and I am not sure about how to build NixOS.

This is how the project looks like:

$ tree -L 1
.
├── br0.xml
├── bridge-qemu.sh
├── configure.nix
├── flake.lock
└── flake.nix

This is the flake.nix file

{
  description = "A very basic flake";

  inputs =
    {
      nixpkgs.url = "github:NixOS/nixpkgs/nixos-22.05";
    };

  outputs = { nixpkgs, home-manager, ... }:
    let
      system = "x86_64-linux";
      pkgs = import nixpkgs {
        inherit system;
        config = { allowUnfree = true; };
      };
      lib = nixpkgs.lib;
    in
    {

      nixosConfigurations = {
        generic = lib.nixosSystem {
          inherit system;
          modules = [
            ./configuration.nix
          ];
        };
      };
    };
}

Previously I was building directly the configuration.nix file with this command (everything works fine):

nix-build --out-link /tmp/netboot ./configure.nix

I am trying to do the same with nix build but I get an error:

$ nix build --out-link /tmp/netboot .#generic

warning: Git tree '/home/gianarb/.dotfiles' is dirty
error: flake 'git+file:///home/gianarb/.dotfiles?dir=nixos%2fmachines%2flab-generic-netbooting' does not provide attribute 'packages.x86_64-linux.generic', 'legacyPackages.x86_64-linux.generic' or 'generic'

What am I doing wrong?
Thanks

As the error message states, nix build .#generic will attempt to build 'packages.x86_64-linux.generic', 'legacyPackages.x86_64-linux.generic' or 'generic'.

In your case you have to run nix build .#nixosConfigurations.generic.config.system.build.topLevel (nix build will not recurse into the configruation, as nix-build will).

Or use the much shorter nixos-rebuild build --flake .#generic

1 Like

Which will put the out-link in ./result, so isn’t exactly what you want, and also need to install nixos-rebuild if you’re not on NixOS.

I would probably still skip the out-link in either case, it’s just a symlink. No real reason to put it in /tmp.

Having the outlink at a well known location makes further scripting easier.

I didn’t use nixos-rebuild build because I thought that it targets my current operating system, but I think build does what I want as well! And your suggestions works as well!

Now I have to fix the configuration.nix I am using, similar to this one Netboot - NixOS Wiki

becuase it does not compile and it is impure.

Thanks a lot