Incrementally salvaging stateVersion

Everybody talks about how we needn’t have stateVersion at all, they’ve been talking about that for a long time, but in practice that future isn’t becoming a reality.

This proposal makes it less opaque to the more compentent among users, could potentially be leveraged in CI checks to actually push the existing nixos codebase toward using it less, and could also be leveraged, once conversion was complete, to allow not setting system.stateVersion if the config doesn’t enable any modules which require it. That would remove a significant amount of the issue in itself.

This isn’t either/or with getting rid of stateVersion, it’s the start a viable upgrade path toward that goal, if that goal turns out to be achievable, which also has immediate benefits, whether the goal turns out to be achievable or not. I’m all for it.

6 Likes

Well, yeah… it requires some work to make it happen, and everyone has limited time, myself included. That applies to this proposal too.

I’m not trying to make perfect the enemy or the good, here: I’m saying that in my view the proposed change is more of a lateral move, and that we have the ability to significantly reduce or even eliminate the option with the tools we already have, which I believe would be a comparable amount of work. Both would involve documenting new conventions, spreading knowledge of them among contributors and reviewers, probably adding ratchet checks to ensure monotonic progress, and going through all the existing uses to migrate them.

By default nothing will happen unless someone is motivated to address the problem. If they are, my suggestion is that the same amount of work could deliver greater benefit by moving more directly in the direction of dropping system.stateVersion, while avoiding the drawbacks of encouraging more unnecessary uses of it and increasing the likelihood that people break their systems for little to no benefit by contradicting our general warnings against bumping the value. Implementing a ratchet check to discourage new uses of system.stateVersion entirely should also be easier than requiring it to be used in a specific form.

This is already possible; we do it in Nixpkgs.

To be clear if I thought it was a open research problem to figure out a good way to kill the option off I wouldn’t be suggesting this. I just don’t believe that’s the case; it’s rather a “nobody who cares about the issue and knows how to fix it is putting in the time to do so” thing. We could avoid adding the vast majority of new uses in Nixpkgs immediately, things like conditional package options could be made mandatory with minimal churn, and I’d estimate there’d be maybe five in-tree uses that would require some careful thought to handle after the rest are incrementally addressed.

A proper edition system and migration tooling would be very nice and important in the long run (we already have pain from being unable to do good gradual rollouts of experimental functionality, and as users are well aware our deprecation process is often not the most graceful) but it’s not required to address something that causes a lot of user pain from only a few dozen instances.

Sounds familiar…

(post must be 20 characters)

1 Like

A proposal like this needs a beachhead. Emily has convinced me that the beachhead I thought would be suitable would be better served by not using stateVersion at all, since the replacement is approximately a one-liner. When it is cheap, not using stateVersion is preferable to per-module stateVersion, which itself is preferable to using system.stateVersion directly. (Emily seems to think that the second comparison is ‘lateral’, but I’m not going to pick that fight right now.)

So my current plan is to table this proposal while I implement Emily’s fix for the Seerr case and document it (which I can do without a whole discussion, I think), and repeat that until I find a use of stateVersion that wouldn’t be a one-liner to fix, at which point I’ll bring it back here for someone to show me how that situation should be resolved. And when the answer to that question becomes, ‘Well, you should write this big thing that nobody has the time to write,’ that’s when I’ll pick this proposal up again.


Edit: previously contained a reaction to @llakala’s comment that was, I now think, unjustified.

9 Likes

A migration like this should probably be done in two steps, as an immediate problem with this is that the current service isn’t forwards compatible, and will break on a rollback.

4 Likes

Sorry - to be clear, that was a joke referencing the fact that many long-standing issues in nixpkgs persist for similar reasons. The motivation is often what blocks fixes, not a plan. I agree with Emily’s summary of the situation.

3 Likes

Ah, I apologize then. I jumped to conclusions about your tone, it seems. Thanks for clearing that up with me.

4 Likes

I think it would be good if it was easier to discover which modules rely on stateVersion.

A services.foo.stateVersion with a fallback to system.stateVersion would accomplish that and also would make it easier to “state upgrade” one service at a time. This option would be a good place for a description what the impact of the stateVersion is. In most cases I expect users wouldn’t actually want to set a module stateVersion but rather a package with a fixed major version, but there might be other reasons for having stateVersion as well.

2 Likes

I probably don’t know something, but running grep -r "stateVersion" ./nixos/modules | wc -l on nixpkgs master branch (22c3790e0c06d91708493855678783ecf6d91205) I get 123 which doesn’t seem that much for an issue that has been discussed multiple times. That includes comment lines and most modules are repeat offenders having many mentions of stateVersion, though this crude method isn’t accounting for using stateVersion to set a boolean variable first and then use that variable for further checks (one of these is Seerr). grep -l -r "stateVersion" ./nixos/modules | wc -l which counts only the number of files mentioning stateVersion gives 52.

1 Like

This is a little unfair, or at least it’s missing a valid motivation too: there are reasons when you absolutely need to update stateVersion. For example, I recently had to update the state version because on old versions, you can’t use NixOS containers and podman OCI containers at the same time (as they clash on /var/lib/containers iirc).

Despite ‘you don’t need to update your stateVersion, just leave it alone’ being the advice you read everywhere (particularly when you are looking for how you know when it’s safe to or not, which is not very helpful :-)), on this occasion I really did need to update it.

AFAIK, there are no intermodule dependencies using system.stateVersion — no module assumes that another module will operate in a certain way because of the current value of system.stateVersion — so there’s no reason for it to be this way, other than avoiding the clutter of a lot of individual stateVersion definitions.

and unfortunately — here we are, yes there is, the NixOS container module and the podman OCI container module have interlocks based on the stateVersion in this way, so I’d say this counts as an inter-module dependency (albeit a fairly weak one)

2 Likes

Well, that didn’t take long.

This ends up being tricky and takes me back to my original position of, ‘Migration scripts are hard; let’s do a smaller project that improves things a smaller amount.’

If, in the new version of the module, you change StateDirectory from "old" to "new", then the service (including its ExecStartPre phase) no longer has access to /var/lib/old (all of this is presuming the sandboxing is in effect). So you can’t copy the old files.

You could get around that by setting StateDirectory=old new. You still can’t mv /var/lib/old directly, but you could mv all of its contents. But then you have the forwards-compatibility problem of what happens when you do a rollback. Now /var/lib/old is empty. You can’t simply replace it with a symlink to /var/lib/new for the same reason that you couldn’t mv /var/lib/old. You’d have to copy everything back over during ExecStopPost in the new version module, and even if you trusted that to be good enough in terms of reliability for this use case (which I maybe 70% do?), that could be an annoying amount of copying to do every time you want to stop the service or your system. (Plus you have to move everything back now every time you start the service, just in case a previous generation was used in the interim.)

Contrast this with manual migration: the user can do a sudo mv, knowing that they’re breaking compatibility with their older generations. (I do think that migration instructions, i.e. the specific sudo mv command to run and the warnings about how this breaks compatibility, would be a very good addition to the documentation of the per-module stateVersion option.) So much simpler.

So I’m opening this proposal back up. I’ll fire off a PR in the next few days.

5 Likes

Hmm. From a cursory reading of the code, it seems as if the actual behavior here is that nixos-containers.nix uses stateVersion to ‘get out of the way’ of the other module, and the other module does what it does unconditionally. So maybe you could say that the other module ‘depends’ on the stateVersion that nixos-containers.nix uses, and therefore would have sort of a conceptual dependency on boot.containers.stateVersion if such a thing were to exist, but the weakness here is relevant — this isn’t a blocker on creating boot.containers.stateVersion because the other module doesn’t need to actually reference system.stateVersion or boot.containers.stateVersion to do its thing.

(Was this actually a hard conflict, do you remember, or merely a warning? I ran a test with an old system.stateVersion ("21.11") and was able to use boot.enableContainers and virtualisation.oci-containers.backend = "podman"; and virtualisation.podman.enable, with a warning being printed but no actual conflict between the three implementations.)

I don’t remember if it was ‘technically usable’ in the conflicting state or not. From memory I spent a few hours trying to get the NixOS containers working, being very confused about why they weren’t even though I appeared to be doing everything properly, before finally settling on the stateVersion change, but I’m not sure if it was a red herring.

As you say, it appears that it’s just a warning — albeit with scary ‘unsupported’ wording and once you know the fact that they share the same storage directory, I think you’d have to be a bit ‘brave’ to allow it to run in that state anyway. (If it breaks, aren’t you going to feel silly asking for support with something that you were told is unsupported?)

Alas, there is no information on when it’s safe because it’s impossible to define that with the way it’s currently implemented, you always have to read the source code of all modules you’re using.

That is also exactly why people are telling you not to change it.

I think the “correct” solution here is to file a nixpkgs bug. You should never have been put into this position in the first place.

3 Likes

Yes first thought, including Nextcloud and Postgres. Migrating them to such a new pattern would just complicate things. Just take the package default out of them and make a good entry in the changelog/assertion that people know based on their stateVersion what they should set.

mv is not atomic and an interuption there could loose or mess up your data which is more likely if you have 100+ GB of data.

4 Likes

This is only a problem moving across file systems, when it has to copy hierarchies one file at a time. This is not what it does when moving within one file system, even when it’s a directory being moved. In that case, it depends on what exactly you mean by atomic, but it is atomic in the way that matters. If the rename operation fails, then nothing will have changed. If it succeeds, then obviously the rename will have occurred.

POSIX.1-2024 makes this pretty explicit:

All file system operations that read or search a directory or that modify the contents of a directory (for example creating, unlinking, or renaming a file) shall operate atomically. That is, each operation shall either have its entire effect and succeed, or shall not affect the file system and shall fail.

But older POSIX versions make it clear enough as well:

Upon successful completion, the rename() function shall return 0. Otherwise, it shall return -1, errno shall be set to indicate the error, and neither the file named by old nor the file named by new shall be changed or created.

The more interesting question is whether it’s atomic with respect to what other threads see, and that’s more complicated.

a directory entry named new shall remain visible to other threads throughout the renaming operation and refer either to the file referred to by new or old before the operation began.

We get a guarantee that the change to an existing file that’s replaced by the rename is atomic to other threads, but we get no such guarantees about when the old name disappears relative to when the new name changes. But I don’t think this is relevant. Although this means it’s not fully atomic to other threads (and indeed this linux documentation suggests it “probably” isn’t), that has no bearing on the safety of the data.

The actually interesting thing w.r.t. data loss is the durability of it. And in the durability sense, essentially nothing you can do with a POSIX file system is atomic. The only durability guarantee you get from POSIX is that fsync’d changes are durable. But anything between fsyncs can either have not happened, partially happened, or broken the file system. In practice, real world implementations are good about operations like a rename within the same directory being either completely durable or not at all, i.e. atomically durable, so to speak.

It’s possible I’m missing something here; I’m no expert on POSIX. But from what I can tell, mv is atomic in all the ways that matter.

2 Likes

I mean, that’s not unlikely, given these folders might be huge and live on other disks/partitions. I think using mv for this remains pretty risky.

1 Like

The command we’re debating was mv /var/lib/{jellyseerr,seer}. This is necessarily on the same file system, since they’re in the same directory.

What if one of them is a mount point?

A mountpoint can’t be moved. And, fair, that particular syntax would allow moving the non-mountpoint old name inside the directory at the target argument. But that’s a scripting mistake; even ignoring the issue with the file copy, moving inside the other directory would have clearly been a mistake, so that would have had to have been written such that moving inside the other directory wasn’t one of the possible outcomes; only moving to the new name. In that scenario, you cannot move to overwrite a mountpoint, so again the issue is moot.