Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[OS-Release] Set image_id to NixOS-based system build toplevel on NixOS systems #3223

Open
wants to merge 6 commits into
base: master
Choose a base branch
from

Conversation

mgrzybek
Copy link

@mgrzybek mgrzybek commented Jan 8, 2025

Context

  • NixOS is based on derivations. The system's state produces a unique hash.
  • image_id is empty on NixOS systems.

Why is it useful?

While managing upgrades, checking the drift is very important. Comparing the live image_id with the desired state from some nix-build evaluation is very practical.

This is why this patch checks if we are running on a NixOS system (NAME env variable) and gets the hash from the symlink at /run/current-system.
The test grabs the hash from the local NixOS system or creates a fake

@mgrzybek
Copy link
Author

mgrzybek commented Jan 8, 2025

Hi @SuperQ and @discordianfish,

This PR is ready for human validation. The changes are quite small, I hope the code is fine for you.

In the end I removed some content from os_release_test.go because reading/creating a fake /run/current-system is complicated.

Regards,
Mathieu

@mweinelt
Copy link

mweinelt commented Jan 8, 2025

NixOS Infra uses a small python script, that exposes various things as a textfile. Ideally we could extract, package and expose that with node-exporter downstream in nixpkgs.

https://github.com/NixOS/infra/tree/main/modules/prometheus/nixos-exporter

@mgrzybek
Copy link
Author

mgrzybek commented Jan 9, 2025

I managed to adapt my code in order to get the data directly from the nix store sqlite database:

  • creating some fixtures: fake symlinks and a nix store DB
  • making some paths configurable when testing

However the make common-unused target prevents me from adding a dependancy into go.mod. Basically I need github.com/mattn/go-sqlite3.

Is it acceptable to add this dependancy? What's the procedure?

@SuperQ
Copy link
Member

SuperQ commented Jan 9, 2025

This seems like an issue for NixOS, it needs to include an IMAGE_ID in the os-release file.

@mweinelt
Copy link

mweinelt commented Jan 9, 2025

@mgrzybek
Copy link
Author

mgrzybek commented Jan 9, 2025

I'm not a NixOS expert but you seem to tell me that I can set IMAGE_ID and IMAGE_VERSION using my nix configuration.

Such as:

system.image = {
  version = system.build.toplevel; # Using lib.strings.splitString to extract the hash
  id = "";
}

Is it right?

@mweinelt
Copy link

mweinelt commented Jan 9, 2025

Yes, that looks correct. I suppose we really only set it for actual images.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

3 participants