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

nixos/modules/flake/source-info: new module #179772

Open
wants to merge 3 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
38 changes: 38 additions & 0 deletions nixos/doc/manual/configuration/flake-source-info.section.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,38 @@
# Flake Configuration Source Information {#sec-flake-source-info}

Perhaps the most significant advantage of a flake-based configuration
over a legacy configuration is the ability to record the revision of
the repository from which a NixOS system is built, as shown in the
following example:

```
$ nixos-version --json
{"configurationRevision":"f25d2e456a5a7b2798620e41e183739fdaf057ed"
,"nixosVersion":"22.11.20220715.a2ec1c5"
,"nixpkgsRevision":"a2ec1c5fca5f131a26411dd0c18a59fcda3bc974"}
```

To enable this feature, set
[](#opt-system.nixos.configuration.sourceInfo) to the source
information of the flake:

```nix
{
inputs.nixpkgs.url = ...;
outputs = { self, nixpkgs }: {
nixosConfigurations = {
host = nixpkgs.lib.nixosSystem {
modules = [
{ system.nixos.configuration.sourceInfo = self.sourceInfo; }
] ++ ...;
};
};
};
}
```

Note that this feature is not enabled by default. That is because in
such a case that the repository changes but a specific system's
configuration, `host` in the above snippet for example, does not
change, there could be extra unnecessary generations. Keep this in
mind while using it.
43 changes: 43 additions & 0 deletions nixos/modules/flake/source-info.nix
Original file line number Diff line number Diff line change
@@ -0,0 +1,43 @@
{ config, lib, ... }:

with lib;

let sourceInfo = config.system.nixos.configuration.sourceInfo;

in
{
options = {

system.nixos.configuration.sourceInfo = mkOption {
type = types.attrsOf types.anything;
default = {};
example = lib.literalExpression ''
{
inputs.nixpkgs.url = ...;
outputs = { self, nixpkgs }: {
nixosConfigurations = {
host = nixpkgs.lib.nixosSystem {
modules = [
{ system.nixos.configuration.sourceInfo = self.sourceInfo; }
] ++ ...;
};
};
};
}
'';
description = lib.mdDoc ''
The source information of a flake-based NixOS configuration.
If set, the attribute `configurationRevision` will appear
properly in the output of `nixos-version --json`.

**Caution:** Setting this option may result in unnecessary
re-deployments if the flake repository changes but the
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
re-deployments if the flake repository changes but the
boot loader entries or re-deployments if the flake repository changes but the

specific system configuration does not change.
'';
};
};

config = {
system.configurationRevision = mkIf (sourceInfo ? rev) sourceInfo.rev;
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Can we set it to "dirty" when we see that sourceInfo is from git but does not have rev?
We don't have a type-like attribute in there (yet?), but the presence of submodules should give it away.

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The intention of setting it to dirty, to my understanding, is to help identify the state of the configuration better. To answer this question well, I checked the relevant code, and have two proposals:

  1. We can do it for all supported VCSes, not just git. For instance, mercurial is also supported (albeit immature). This is partly due to only git getting special handling while examining local paths, and we may improve that too to enable support for other VCSes. This also implies a standardized, fine-grained specification of source information, as will be discussed in sourceInfo does not tell us whether it's git or not nix#6747.

  2. To further tell differences among dirty configurations, we can fetch the latest update time of a repository and set something like dirty-<timestamp>. The latest update time can be defined as the most recent modification time of all files in the repository.

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@roberth Do we solve this later as a part of NixOS/rfcs#125?

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

We can scope this discussion out regardless.

};
}
1 change: 1 addition & 0 deletions nixos/modules/module-list.nix
Original file line number Diff line number Diff line change
Expand Up @@ -38,6 +38,7 @@
./config/users-groups.nix
./config/vte.nix
./config/zram.nix
./flake/source-info.nix
./hardware/acpilight.nix
./hardware/all-firmware.nix
./hardware/bladeRF.nix
Expand Down