specialisation: limit the allowed characters in specialisation names

Since the systemd boot counting PR was merged, dashes in specialisation
names cause issues when installing the boot loader entries, since dashes
are also used as separator for the different components of the file name
of the boot loader entries on disk.

The assertion avoids this footgun which is pretty annoying to recover
from.
This commit is contained in:
r-vdp 2024-08-11 19:03:05 +02:00
parent 5d06d0d8df
commit 57a30e4cbd
No known key found for this signature in database
2 changed files with 51 additions and 1 deletions

View File

@ -1,10 +1,14 @@
{ config, lib, pkgs, extendModules, noUserModules, ... }:
{ config, lib, extendModules, noUserModules, ... }:
let
inherit (lib)
attrNames
concatStringsSep
filter
length
mapAttrs
mapAttrsToList
match
mkOption
types
;
@ -73,6 +77,19 @@ in
};
config = {
assertions = [(
let
invalidNames = filter (name: match "[[:alnum:]_]+" name == null) (attrNames config.specialisation);
in
{
assertion = length invalidNames == 0;
message = ''
Specialisation names can only contain alphanumeric characters and underscores
Invalid specialisation names: ${concatStringsSep ", " invalidNames}
'';
}
)];
system.systemBuilderCommands = ''
mkdir $out/specialisation
${concatStringsSep "\n"

View File

@ -71,6 +71,32 @@ import ./make-test-python.nix ({ pkgs, ... }: {
}
'';
wrongConfigFile = pkgs.writeText "configuration.nix" ''
{ lib, pkgs, ... }: {
imports = [
./hardware-configuration.nix
<nixpkgs/nixos/modules/testing/test-instrumentation.nix>
];
boot.loader.grub = {
enable = true;
device = "/dev/vda";
forceInstall = true;
};
documentation.enable = false;
environment.systemPackages = [
(pkgs.writeShellScriptBin "parent" "")
];
specialisation.foo-bar = {
inheritParentConfig = true;
configuration = { ... }: { };
};
}
'';
in
''
machine.start()
@ -116,5 +142,12 @@ import ./make-test-python.nix ({ pkgs, ... }: {
with subtest("Make sure nonsense command combinations are forbidden"):
machine.fail("nixos-rebuild boot --specialisation foo")
machine.fail("nixos-rebuild boot -c foo")
machine.copy_from_host(
"${wrongConfigFile}",
"/etc/nixos/configuration.nix",
)
with subtest("Make sure that invalid specialisation names are rejected"):
machine.fail("nixos-rebuild switch")
'';
})