News for hackers. Stuff that matters.
Re:NixOS 21.05 by alias_neo on Wednesday June 02, 2021 @11:31AM (#27369842)
So if I understand correctly, I could declaratively specify my workstation, say I want vim, podman, codium, kubectl, curl, tmux, firefox, zettlr. I could put this in a configuration and have my workstation anywhere I can grab that from?

Is there some way I can boot-to-git-configuration? So I could do this in RAM on someone else's hardware, like a network boot?

Re:NixOS 21.05 by Sleepytime on Wednesday June 02, 2021 @12:21PM (#27370540)
>Is there some way I can boot-to-git-configuration?

Yes, the NixOS installer is a live disk with all the features of NixOS. I have booted configurations directly from IPFS in the installer, and many nix commands and functions support git (or GitHub) natively.

You can declaratively specify your whole infrastructure from software configuration to remote machines with their own VMs.

For example here's my nix function for matrix: https://0x0.st/-2CA.nix

It does a whole bunch of stuff.

First it reads in the hostname and domain from the system configuration and put that in a variable to be used when configuring the subdomains later.

Then it opens up your firewall, starts a postgres service, creates the matrix db/user, configures nginx for matrix.hortname.domain, gets your SSL certificate (and sets up automatic renewal), sets up matrix, and lastly a hosted web client at element.host.domain. All services run under their own users on the host system.

It's zero configuration, I just import that to any machine that I want and it's running a matrix server with all appropriate ancillary configuration done.

Re:NixOS 21.05 by flatiron on Wednesday June 02, 2021 @01:36PM (#27371615)
i hope you aren't still using that shared secret
Re:NixOS 21.05 by pxc on Wednesday June 02, 2021 @03:21PM (#27372850)
Nix's IPFS support is for everything in the Nix store, which is world-readable and in which NixOS places no secrets. Options for managing secrets in/with Nix projects either encrypt them in the store or never have them touch the store (encrypted or otherwise).

Mutable state like database contents, including their password databases, don't go in the Nix store either.

Re:NixOS 21.05 by GordonS on Wednesday June 02, 2021 @01:43PM (#27371701)
Or the password for that Postgres user...
Re:NixOS 21.05 by alias_neo on Wednesday June 02, 2021 @03:30PM (#27372951)
Thanks I'll have a read through it, I can lots of uses for this already!
Re:NixOS 21.05 by Nullabillity on Wednesday June 02, 2021 @11:58AM (#27370224)
> So if I understand correctly, I could declaratively specify my workstation, say I want vim, podman, codium, kubectl, curl, tmux, firefox, zettlr. I could put this in a configuration and have my workstation anywhere I can grab that from?

Kind of. It works very well if you own multiple computers that you want to be configured similarly (like a dotfiles repo on steroids).

It doesn't work so well for I borrowed my coworker's computer for 5 minutes and want to use my own Vim configuration.

However, Nix does also supply the "nix run" command, which allows you to use software without installing it permanently (but without NixOS' configuration support).

> Is there some way I can boot-to-git-configuration? So I could do this in RAM on someone else's hardware, like a network boot?

Yes. You could either build an image with a service that git clones your configuration and switches to it, or you could build an image containing a prebuilt snapshot of your configuration.

Either approach should be possible for basically any medium (USB drive, DVD, netboot, whatever). That said, for the build-and-switch setup I'd recommend something that can give at least /nix/store a writable partition (to avoid having to rebuild/redownload the whole world on every reboot).

Re:NixOS 21.05 by Sleepytime on Wednesday June 02, 2021 @12:31PM (#27370694)
>It doesn't work so well for I borrowed my coworker's computer for 5 minutes and want to use my own Vim configuration

You could use something like nixos-shell[1] to spin up a headless VM of your machine into your current shell.

[1]https://github.com/Mic92/nixos-shell

Re:NixOS 21.05 by gmfawcett on Wednesday June 02, 2021 @01:57PM (#27371878)
Well that is pretty neat. Thanks for the mention of nixos-shell. I've been a NixOS daily-driver for the past 6 months or so, but I am still picking up new ideas on a daily basis. :)
Re:NixOS 21.05 by Sleepytime on Friday June 04, 2021 @10:57PM (#27401672)
It's been my daily for not much longer than that, and almost every time I play with NixOS itself I get the sense of wonder and possibility in computers that I haven't had since I was a teenager. It's definitely a force multiplier.
Re:NixOS 21.05 by alias_neo on Wednesday June 02, 2021 @03:32PM (#27372984)
Ive seen mention to "switching" to a configuration a few times now, what does that mean technically? Is it a reboot and select a different grub option, does it take time/have to download things? What really is "switching"?
Re:NixOS 21.05 by Gaelan on Wednesday June 02, 2021 @04:50PM (#27373858)
Each NixOS machine has a current configuration, symlinked from /run/current-system. For example, my NixOS laptop currently has this:

    /run/current-system -> /nix/store/95n5xr5n8bw6xbab8fiz6cqc6mydryg8-nixos-system-Sweetpea-21.11pre291991.ea7d4aa9b82
My $PATH contains /run/current-system/sw/bin, so all of my machine's (globally-installed) software comes from whatever the current configuration is. There's also some other stuff in there, like /run/current-system/kernel and /run/current-system/etc, which contain what you'd expect.

Let's say I want to change something. I edit /etc/nixos/configuration.nix, then run "nixos-rebuild build", which produces this:

    /nix/store/0fhxjqbv20psbkjvw70wni9ww2cxfqgm-nixos-system-Sweetpea-21.11pre292748.6933d068c5d
That step does all of the downloading, etc, but it doesn't actually change anything on my system—the /run/current-system symlink hasn't changed, so I'm still running on the old configuration.

When I'm ready to change, I run "/nix/store/0fhxj.../bin/switch-to-configuration". This step actually changes the /run/current-system symlink, updates /etc, restarts any daemons that were updated, etc. Obviously, if your configuration has a new kernel, that won't be applied until you reboot.

In practice, we almost always just run "nixos-rebuild switch", which does the build and switch in one go.

This whole process is entirely idempotent and reversible; I can switch back to any configuration I've ever built (unless I've since removed them to save space, of course). GRUB also displays a list of every configuration you've ever built, so you can always boot into a stable configuration even if you've somehow made a royal mess of things.

Re:NixOS 21.05 by alias_neo on Wednesday June 02, 2021 @06:57PM (#27375055)
Thanks for taking the time to describe that, it makes sense now.

I'm impressed that it manages restarting everything on the fly without a reboot on a running system.

Re:NixOS 21.05 by pxc on Thursday June 03, 2021 @12:12AM (#27377144)
On rare occasions, there can be problems restarting a service, like if the service in the new configuration in fact relies on a newer kernel module than what you've got in your running configuration. In addition, sometimes Nix's garbage collector won't dispose of something because it can tell you've still got some processes running that use it or something like that, and so there's a bit of space you can't free up until after reboot. And of course, kernel changes still require reboots on NixOS.

All in all, the service management stuff is handled very gracefully imo. I've never run into real trouble with it (and one time I had a power outage in the middle of a `nixos-rebuild switch`).

Re:NixOS 21.05 by Nullabillity on Wednesday June 02, 2021 @04:48PM (#27373833)
Building the new configuration (and downloading or building dependencies), adding the bootloader entry, and activating the new configuration (stopping removed services, starting new services, reloading changed services, that kind of thing).

You don't need to do all three: you can skip the bootloader if you want to try something temporarily, and you can skip activation if you want to wait with that until the next reboot. There are also some changes that can't be activated without a reboot, such as changing the kernel.

> Does it take time/have to download things?

Building has to download or build stuff that isn't in the cache. But contrary to, say, Docker it understands the build graph, so it only rebuilds actual dependees.

Activation depends on how many services you've changed, and how quickly they restart. Updating the bootloader is nearly instant.

Re:NixOS 21.05 by alias_neo on Wednesday June 02, 2021 @06:59PM (#27375068)
Thats pretty incredible. So I can work on configs, switch back and forth until it's exactly right and then switch to it permanently.

Where's a good place to start? Some of the configs people are throwing about are quite involved and look like they need a bunch of specialist knowledge.

Thanks for taking the time to explain. Can't wait to start trying this out.

Re:NixOS 21.05 by pxc on Thursday June 03, 2021 @12:23AM (#27377201)
Just take a few minutes to skim the NixOS manual, then keep it in front of you on another device while you do an install. Enable a web browser, a desktop environment, and NetworkManager in your initial configuration.nix.

Then just start adding software that you want to be able to use. Search for it first in the NixOS options[1]. If you see it there, enable it and configure it using the options that make sense to you. If you don't see it there, search for it in the package collection[2], then use the name you find for it there to add it to `environment.systemPackages` in your `configuration.nix` file.

Just kinda take it easy and continue with that process until you have an environment that's more or less comfy. Then you're free to revisit your config and think about stuff like tying it in with your dotfiles, using Nix to manage your home directory, services you might want to run, etc.

—

1: https://search.nixos.org/options?channel=21.05&

2: https://search.nixos.org/packages?channel=21.05&

Re:NixOS 21.05 by kaba0 on Wednesday June 02, 2021 @02:44PM (#27372388)
> However, Nix does also supply the "nix run" command, which allows you to use software without installing it permanently (but without NixOS' configuration support).

What do you mean without config support? It runs a nix derivation which can be arbitrarily complex. Do you mean your /etc/nixos/configuration.nix file? Because you are not really using it either, only for creating a derivation that will become your system. A nix-shell of a user will by default use the user’s channel and may not do anything with the global config.

Re:NixOS 21.05 by pxc on Wednesday June 02, 2021 @03:23PM (#27372874)
Yeah, I think they just mean without the conveniences of the NixOS module system. You can definitely bundle config with an app and store it in a repository's shell.nix or a flake or whatever.
Re:NixOS 21.05 by e3bc54b2 on Wednesday June 02, 2021 @11:46AM (#27370041)
Alternative to what others have said, you can use your config to build a custom bootable ISO and use it to setup your new system. Comes very handy if you can't guarantee nice network for setup.
Re:NixOS 21.05 by SkyMarshal on Wednesday June 02, 2021 @01:27PM (#27371486)
Last time I tried this, the NixOS Live USB still requires network connectivity to install. Is there some way to create an ISO or Live USB that installs completely offline?
Re:NixOS 21.05 by takeda on Wednesday June 02, 2021 @03:19PM (#27372811)
Nix kind of expects to be always connected.

It is possible though, if you have the same config and use channels at the same revision (or use flakes) nix will know all packages it needs. You could fetch them and then either provide that machine as a cache, or use nix copy to copy the packages.

Re:NixOS 21.05 by alias_neo on Wednesday June 02, 2021 @11:54AM (#27370161)
This sounds like a great idea. Do you happen to know of any documentation to assist with this? I'd love to create a custom, bootable USB which I can upgrade periodically, keeping my SSH keys on a Yubikey.
Re:NixOS 21.05 by zimbatm on Wednesday June 02, 2021 @11:58AM (#27370223)
One way is to use this project: https://github.com/nix-community/nixos-generators

It's just a small wrapper but it shows you most of the targets available.

Re:NixOS 21.05 by alias_neo on Wednesday June 02, 2021 @12:05PM (#27370298)
Great, thanks!
Re:NixOS 21.05 by mason55 on Wednesday June 02, 2021 @11:40AM (#27369969)
> So if I understand correctly, I could declaratively specify my workstation, say I want vim, podman, codium, kubectl, curl, tmux, firefox, zettlr. I could put this in a configuration and have my workstation anywhere I can grab that from?

Yes, and you can specify your .vimrc, your vim plugins, the users you want setup, their SSH keys, your systemd config, etc.

> Is there some way I can boot-to-git-configuration?

Bootstrapping a system is the one place where you still need to do a bit of work, but it's not bad.

I personally have a script in my Nix config repo which pulls my config down to disk, sets up symlinks, and then switches to the new config. From there everything then goes through standard Nix tooling.

Re:NixOS 21.05 by Filligree on Wednesday June 02, 2021 @11:42AM (#27370001)
What you can relatively easily do is extend the 'installer' config with whatever you want to add there, for instance applications and a desktop environment.

It should be relatively straightforward to also mount /home from a separate USB stick partition, but I haven't tried.

Re:NixOS 21.05 by mason55 on Wednesday June 02, 2021 @12:10PM (#27370385)
Oh yeah, that's not a bad idea and definitely what I'd do if I was bootstrapping a bunch of hosts at once. As it is I set up maybe one new host a year, which means that it's at least as much work to rebuild an installer boot media + my config as it is to do a base install and then pipe a script through bash to finish setup.
Re:NixOS 21.05 by alias_neo on Wednesday June 02, 2021 @11:49AM (#27370086)
Amazing thanks. I'm going to start taking a look at getting a simple configuration started with my basic setup, keys, shell, tmux config etc.
Re:NixOS 21.05 by foolmeonce on Wednesday June 02, 2021 @06:05PM (#27374608)
With nixops you also define define your configurations in nix (and can have interdependencies between them) then start machines or virtual machines as blank nixos installs and add their ssh key and address for nixops to reconfigure and manage.

I ultimately find it easier to have one host that is keeping track of all configurations and pushing my changes to all hosts since I otherwise end up with a lot of machines with different changes I've been working on.

Re:NixOS 21.05 by Blikkentrekker on Wednesday June 02, 2021 @11:54AM (#27370166)
Almost any system can do that.

Package managers typically have a concept of a “selected set” or something o that nature which is often simply a file that contains all the packages the user wants to install on a new line at some places. Simply copy that file and do a world update with the package manager and it will install all those packages an their dependencies and typically uninstall what is not needed.

Re:NixOS 21.05 by Nullabillity on Wednesday June 02, 2021 @12:04PM (#27370292)
NixOS also covers package configuration, not just which packages are installed. And because it owns all of the configuration, it can semi-intelligently merge package configurations that are managed in multiple independent places.

It can also be used to apply patches to packages (which are then automatically carried over when the upstream package is updated).

Re:NixOS 21.05 by Blikkentrekker on Thursday June 03, 2021 @09:14AM (#27380774)
> NixOS also covers package configuration, not just which packages are installed. And because it owns all of the configuration, it can semi-intelligently merge package configurations that are managed in multiple independent places.

Do you mean system configuration in /etc, or also user configuration in /home?

In the latter case I do not see how this can ever be implemented, even if an exhaustive list be provided with each package in it's description, quite a bit of software will arbitrarily add new files under new names as part of it's confguration.

Usually one simply copies one's entire `~/.config` directory along.

> It can also be used to apply patches to packages (which are then automatically carried over when the upstream package is updated).

True, but this is also a feature of any source-based system.

The real advantage to me seems to be that it is a source-based system but with a central repository for identically configured packages to avoid compilation in many cases.

Re:NixOS 21.05 by Nullabillity on Thursday June 03, 2021 @10:23AM (#27381803)
> Do you mean system configuration in /etc, or also user configuration in /home?

NixOS covers /etc, and Home-Manager covers /home. But in practice many applications support configuration from /etc as well, and you will tend to prefer just managing it from there when using NixOS.

> In the latter case I do not see how this can ever be implemented, even if an exhaustive list be provided with each package in it's description, quite a bit of software will arbitrarily add new files under new names as part of it's confguration.

Hm? If you use Nix to manage the application's configuration then Nix is the only program allowed to change the configuration directly.

> Usually one simply copies one's entire `~/.config` directory along.

Not the same thing. Nix allows you to express relationships between different application configurations (why does program A try to connect to localhost:9267? oh, ripgrep says that program B is binding that port for RPC...), and lets you stack configurations (so that defaults can be updated automatically, or you can change a specific value for a specific machine without having to maintain two completely separate copies).

It's like the difference between giving a new employee access to your source code, vs saying "here's an optimized stripped build, and here's a copy of Ghidra, have fun!".

> True, but this is also a feature of any source-based system.

In the same sense that you can copy your dotfiles around manually, sure. The difference is that Nix allows you to patch the package definition itself dynamically. AUR/PKGBUILD (the only other source-based system I'm familiar with to be fair, not sure if Gentoo handles this differently) allows you to patch the /application/, but you still need to copy the PKGBUILD and backport all upstream changes.

Re:NixOS 21.05 by pxc on Wednesday June 02, 2021 @03:30PM (#27372956)
*Almost any system can attempt that.

Nix lets you pin your whole dependency tree, and Nixpkgs includes facilities for managing the configurations of many applications, including integrations with their plugin systems that explicitly manage external runtime dependencies, like stuff your vim configuration might call at the CLI. Without that stuff, reproduction can often fail in practice.

And in the case of NixOS, you can also reproduce things like running services and their configurations, which users exist, etc. Those are OS and configuration management features, not package management.