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

Rename nixosConfigurations to configurations.<system> #10291

Open
wants to merge 1 commit 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
27 changes: 27 additions & 0 deletions doc/manual/rl-next/nixos-configurations.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
---
synopsis: "Renamed `nixosConfigurations.`*name* to
`configurations.`*system*`.`*name*"
prs: 10291
---

The `nixosConfigurations.`*name* output is deprecated in favor of
`configurations.`*system*`.`*name* where *system* the name of the system that
would be used to build the configuration, similar to `packages`, `checks` and
other system-dependent flake outputs that produce derivations.

The old output will continue to work, but `nix flake check` will issue a
deprecation warning about it.

It is strongly recommended to keep configurations identical when defined under
the same name with multiple systems, e.g.

* `configurations.aarch64-linux.webserver` defines a webserver configuration
built on and for `aarch64-linux` system.
* `configurations.x86_64-linux.webserver` describes the same configuration, but
cross-compiled from `x86_64-linux` system.

Note though that bit-wise identical cross-compilation is currently not
possible without experimental [content-addressed derivations] (and even then a
lot of effort is necessary on the Nixpkgs side to make that work).

[content-addressed derivations]: @docroot@/contributing/experimental-features.md#xp-feature-ca-derivations
2 changes: 1 addition & 1 deletion src/nix/build.md
Original file line number Diff line number Diff line change
Expand Up @@ -64,7 +64,7 @@ R""(

```console
# nix build --profile /nix/var/nix/profiles/system \
~/my-configurations#nixosConfigurations.machine.config.system.build.toplevel
~/my-configurations#.configurations.x86_64-linux.machine.config.system.build.toplevel
```

(This is essentially what `nixos-rebuild` does.)
Expand Down
8 changes: 7 additions & 1 deletion src/nix/flake-check.md
Original file line number Diff line number Diff line change
Expand Up @@ -34,8 +34,10 @@ The following flake output attributes must be derivations:
* `defaultPackage.`*system*
* `devShell.`*system*
* `devShells.`*system*`.`*name*
* `nixosConfigurations.`*name*`.config.system.build.toplevel`
* `packages.`*system*`.`*name*
* `nixosConfigurations.`*name*`.config.system.build.toplevel`
* `configurations.`*system*`.`*name*`.config.system.build.toplevel` if
`configurations.`*system*`.`*name*`.class` is `nixos`

The following flake output attributes must be [app
definitions](./nix3-run.md):
Expand All @@ -49,6 +51,10 @@ definitions](./nix3-flake-init.md):
* `defaultTemplate`
* `templates.`*name*

The following flake output attributes must be *configurations*:

* `configurations.`*system*`.`*name*

The following flake output attributes must be *Nixpkgs overlays*:

* `overlay`
Expand Down
88 changes: 81 additions & 7 deletions src/nix/flake.cc
Original file line number Diff line number Diff line change
Expand Up @@ -506,21 +506,55 @@ struct CmdFlakeCheck : FlakeCommand
}
};

auto checkNixOSConfigurationToplevel = [&](Value & v, const PosIdx pos) {
Bindings & bindings(*state->allocBindings(0));
auto vToplevel = findAlongAttrPath(*state, "config.system.build.toplevel", bindings, v).first;
state->forceValue(*vToplevel, pos);
if (!state->isDerivation(*vToplevel))
throw Error("attribute 'config.system.build.toplevel' is not a derivation");
};

auto checkNixOSConfiguration = [&](const std::string & attrPath, Value & v, const PosIdx pos) {
try {
Activity act(*logger, lvlInfo, actUnknown,
fmt("checking NixOS configuration '%s'", attrPath));
Bindings & bindings(*state->allocBindings(0));
auto vToplevel = findAlongAttrPath(*state, "config.system.build.toplevel", bindings, v).first;
state->forceValue(*vToplevel, pos);
if (!state->isDerivation(*vToplevel))
throw Error("attribute 'config.system.build.toplevel' is not a derivation");
checkNixOSConfigurationToplevel(v, pos);
} catch (Error & e) {
e.addTrace(resolve(pos), HintFmt("while checking the NixOS configuration '%s'", attrPath));
reportError(e);
}
};

auto checkConfiguration = [&](const std::string & attrPath, Value & v, const PosIdx pos) {
try {
Activity act(*logger, lvlInfo, actUnknown,
fmt("checking configuration '%s'", attrPath));
state->forceAttrs(v, pos, "");
auto sType = state->symbols.create("_type");
auto aType = v.attrs->get(sType);
if (aType && aType->name == sType) {
auto aTypeValue = state->forceStringNoCtx(*aType->value, aType->pos, "");
if (aTypeValue == "configuration") {
auto sClass = state->symbols.create("class");
auto aClass = v.attrs->get(sClass);
if (aClass && aClass->name == sClass) {
auto aClassValue = state->forceStringNoCtx(*aClass->value, aClass->pos, "");
// Check for v.class == "nixos". This exists mostly for compatibility
// with configurations migrated from nixosConfigurations.
if (aClassValue == "nixos") {
checkNixOSConfigurationToplevel(v, pos);
}
}
return; // OK
}
}
throw Error("attribute set is not a configuration");
} catch (Error & e) {
e.addTrace(resolve(pos), HintFmt("while checking the configuration '%s'", attrPath));
reportError(e);
}
};

auto checkTemplate = [&](const std::string & attrPath, Value & v, const PosIdx pos) {
try {
Activity act(*logger, lvlInfo, actUnknown,
Expand Down Expand Up @@ -594,6 +628,7 @@ struct CmdFlakeCheck : FlakeCommand
name == "overlay" ? "overlays.default" :
name == "devShell" ? "devShells.<system>.default" :
name == "nixosModule" ? "nixosModules.default" :
name == "nixosConfigurations" ? "configurations.<system>.<name>" :
"";
if (replacement != "")
warn("flake output attribute '%s' is deprecated; use '%s' instead", name, replacement);
Expand Down Expand Up @@ -725,6 +760,21 @@ struct CmdFlakeCheck : FlakeCommand
*attr.value, attr.pos);
}

else if (name == "configurations") {
state->forceAttrs(vOutput, pos, "");
for (auto & attr : *vOutput.attrs) {
const auto & attr_name = state->symbols[attr.name];
checkSystemName(attr_name, attr.pos);
if (checkSystemType(attr_name, attr.pos)) {
state->forceAttrs(*attr.value, attr.pos, "");
for (auto & attr2 : *attr.value->attrs)
checkConfiguration(
fmt("%s.%s.%s", name, attr_name, state->symbols[attr2.name]),
*attr2.value, attr2.pos);
};
}
}

else if (name == "hydraJobs")
checkHydraJobs(name, vOutput, pos);

Expand Down Expand Up @@ -1154,6 +1204,7 @@ struct CmdFlakeShow : FlakeCommand, MixJSON
|| attrPathS[0] == "checks"
|| attrPathS[0] == "devShells"
|| attrPathS[0] == "legacyPackages"
|| attrPathS[0] == "configurations"
|| attrPathS[0] == "packages")
&& (attrPathS.size() == 1 || attrPathS.size() == 2)) {
for (const auto &subAttr : visitor2->getAttrs()) {
Expand Down Expand Up @@ -1257,6 +1308,26 @@ struct CmdFlakeShow : FlakeCommand, MixJSON
}
};

auto showConfiguration = [&]()
{
auto aType = visitor.maybeGetAttr("_type");
if (!aType || aType->getString() != "configuration")
state->error<EvalError>("not a configuration definition").debugThrow();

std::optional<std::string> optClass;
if (auto aClass = visitor.maybeGetAttr("class"))
optClass = aClass->getString();

if (json) {
j.emplace("type", "configuration");
if (optClass)
j.emplace("class", *optClass);
} else if (optClass)
logger->cout("%s: %s configuration", headerPrefix, *optClass);
else
logger->cout("%s: configuration", headerPrefix);
};

if (attrPath.size() == 0
|| (attrPath.size() == 1 && (
attrPathS[0] == "defaultPackage"
Expand All @@ -1270,6 +1341,7 @@ struct CmdFlakeShow : FlakeCommand, MixJSON
|| ((attrPath.size() == 1 || attrPath.size() == 2)
&& (attrPathS[0] == "checks"
|| attrPathS[0] == "packages"
|| attrPathS[0] == "configurations"
|| attrPathS[0] == "devShells"
|| attrPathS[0] == "apps"))
)
Expand All @@ -1279,7 +1351,7 @@ struct CmdFlakeShow : FlakeCommand, MixJSON

else if (
(attrPath.size() == 2 && (attrPathS[0] == "defaultPackage" || attrPathS[0] == "devShell" || attrPathS[0] == "formatter"))
|| (attrPath.size() == 3 && (attrPathS[0] == "checks" || attrPathS[0] == "packages" || attrPathS[0] == "devShells"))
|| (attrPath.size() == 3 && (attrPathS[0] == "checks" || attrPathS[0] == "packages" || attrPathS[0] == "configurations" || attrPathS[0] == "devShells"))
)
{
if (!showAllSystems && std::string(attrPathS[1]) != localSystem) {
Expand All @@ -1289,7 +1361,9 @@ struct CmdFlakeShow : FlakeCommand, MixJSON
logger->warn(fmt("%s omitted (use '--all-systems' to show)", concatStringsSep(".", attrPathS)));
}
} else {
if (visitor.isDerivation())
if (attrPathS[0] == "configurations")
showConfiguration();
else if (visitor.isDerivation())
showDerivation();
else
throw Error("expected a derivation");
Expand Down
2 changes: 2 additions & 0 deletions tests/functional/flakes/show.sh
Original file line number Diff line number Diff line change
Expand Up @@ -50,6 +50,8 @@ cat >flake.nix <<EOF
legacyPackages.$system = { };
packages.$system = { };
packages.someOtherSystem = { };
configurations.$system = { };
configurations.someOtherSystem = { };

formatter = { };
nixosConfigurations = { };
Expand Down
Loading