How to do a flake build in non-nixos system?

Hi,

I have a flake build system successfully deployed on my nixos. But I would like to try on non-nixos systems. I realized nixos-rebuild, nixos-install and darwin-rebuild both has flake support now.

We can do things like this nixos-install --root /mnt --flake ~/.config/dotfiles#<hostname> to install or build the system. But how can we do this when we don’t have nixos-rebuild on a non-nixos system?

Thanks!

1 Like

If nixUnstable is installed and experimental-features = nix-command flakes is set in /etc/nix/nix.conf you can just run nix commands with flakes, like nix build nixpkgs#hello.

1 Like

@NobbZ

Thanks! I tried

nix build try to build a package while kuro is actually my hostname. When we do a flake build system, it builds a host rather a package. Although the concept might be just the same, nix build doesn’t understand it’s a hostname.

1 Like

Well, does the flake provide a package kuro in its outputs?

Perhaps share the flake and tell us what in it you want to build exactly.

Here is the flake file I’m using https://github.com/ztlevi/nix-dotfiles/blob/59a1f3c5d777759fc20cac114a3d5aebb4bd179b/flake.nix

You can find more details in the lib folder, e.g. mapHosts

kuro is the hostname, BTW.

So you wanted to build a configuration? That’s done a bit differently, I do it in my GitHub actions:

Replace {{ matrix.host }} by the actual name of your host.

Note:

  • nixos-rebuild --flake /flake/dir (with no output specified) will try to build .#nixosConfigurations.[hostname].config.system.build.toplevel

  • nixos-rebuild --flake /flake/dir#kuro will try to build .#nixosConfigurations.kuro.config.system.build.toplevel specifically

But… nix build doesn’t do the exact same coercions!

So, if you want to build the “toplevel” yourself and then activate it, you can do this:

nix --experimental-features 'nix-command flakes' build -L \
  '.#nixosConfigurations.kuro.config.system.build.toplevel'`

Then you could do to set it as a new generation in the system profile:

sudo nix build --profile /nix/var/nix/profiles/system "$(readlink -f result)"

And then you could immediately switch to it:

sudo nix shell -vv "$(readlink -f result)" -c switch-to-configuration switch

This is very similar to what nixos-rebuild --flake does.

3 Likes

Thanks!

nix --experimental-features 'nix-command flakes' build -L \ '.#nixosConfigurations.kuro.config.system.build.toplevel' works perfectly. But I don’t know how to switch to the environment after the build is complete…

There are /nix, ~/.nix-profile folder in my ubuntu system, but I didn’t find /run/current-system. Simply restart the os doesn’t give the nix environemnt.

Well, my post explains how to do that. But also, sorry, I guess I just put the pieces together…

What are you expecting to have happen when you activate a NixOS configuration on a non-NixOS system? That’s not something I’d recommend doing.

I’m expecting the packages and desktop configurations are setup based on my config. For example, the window manger and be found in my login manager, e.g. gdm.

I’m curious since we can define the kernel version in nix, does that mean we would have a seperate kernel specifically insatlled for nix build?

I’m having trouble activating the system.

  1. sudo cannot find nix. So I didn’t use sudo in the first command. Note I am in a u

  1. it pops error when I switch to the profile

You can use Nix on Ubuntu, but I would personally avoid activating a NixOS configuration in an Ubuntu install for fear of breaking things.

As I suspected, it does have a safety check to prevent this. I don’t recommend trying to circumvent it.

I see. Is it possible to activate a nix shell environment defined in the profile?

What about nix-darwin. I have a build on darwin as well although that one hasn’t changed to flake build yet. But I am expecting the darwin build will at least has the packages and shell environment defined with home-manger.

I guess I would just generally back up and point out that flakes are just largely some syntactic sugar and don’t fundamentally change what is possible with nix, h-m, or nix-darwin (And I don’t know anything else about nix-darwin, sorry.)

A flake.nix with a devShell is possibly a good strategy for portability and reproducability. This is what I am doing for new software projects. I just booted an Ubuntu VM, installed Nix, installed nixUnstable, and was able to open a shell for my project:

cole@cole-Standard-PC-Q35-ICH9-2009:~$ go version
Command 'go' not found, but can be installed with: ...

# the next command pulls and uses this flake file:
# https://github.com/colemickens/niche/blob/12caadbda9c2b043e32e92900bbef508ef490871/flake.nix
cole@cole-Standard-PC-Q35-ICH9-2009:~$ nix \
  --experimental-features 'nix-command flakes' \
  develop 'github:colemickens/niche/master'

# now I'm inside the develop shell, yay

cole@cole-Standard-PC-Q35-ICH9-2009:~$ go version
go version go1.15.5 linux/amd64

(edit: interestingly, that flake.nix doesn’t contain a devShell attribute, so it looks like Nix went ahead and gave me the shell environment for the default package; kind of nice!)

Thanks! That’s a good example.

I have two more questions before you go…

  1. By saying install nixUnstable, do you mean install the unstable channel? Wasn’t it already defined in flake.nix?
  2. Is it possible to refer two flake builds in one git repo?
    I found this nix-flakes.md · GitHub

Don’t know if it’s still the case. Otherwise, I have to create a separate git repo specifically for my nix-darwin build.

Thank you again for the great help!

When I say nixUnstable there, I mean that the nix installer gave me stable nix, and then I installed a newer nix on top, via the nixUnstable package. So, after installing Nix in Ubuntu, I did this to get a newer version of Nix: nix-env -f https://github.com/nixos/nixpkgs/archive/nixos-unstable.tar.gz -iA nixUnstable.

I think you can only have one flake.nix in the root of a git repo, but you can have multiple outputs, packages, etc. My nixcfg flake defines my laptop, raspberry pis, cloud VMs, and more, from a single flake.nix.

For anyone looking, it seems that after a few years, we now have a solution: GitHub - numtide/system-manager: Manage system config using nix on any distro! :smiley:

1 Like