take this example section of my flake.nix:
...
outputs = { self, nixpkgs, ... }: {
nixosConfigurations = {
arbitrary-name = nixpkgs.lib.nixosSystem {
modules = [
./host/arbitrary-name/configuration.nix
];
};
...
};
};
and this section of host/arbitrary-name/configuration.nix
:
networking.hostname = arbitrary-name;
I want to standardize my configuration more and remove the boilerplate of writing the machine’s hostname multiple times per system, is there a way for me to access the name of the nixosConfiguraiton
entry that the machine was built off of to set its hostname and import its configuration.nix
?
I have something like this in my flake:
...
outputs = { self, nixpkgs, ... }: {
nixosConfigurations = nixpkgs.lib.genAttrs ["arbitrary-name"] (hostName: nixpkgs.lib.nixosSystem {
modules = [
{ networking.hostName = hostName; }
./host/${hostName}/configuration.nix
];
...
});
};
On mobile now so there may be some mistakes in there.
1 Like
Yeah, I think using a home made function to import by name is quite common. I have one inspired by Wimpy’s.
# Flake.nix
nixosConfigurations = {
optiplex3020 = libx.helpers.mkHost {
hostname = "optiplex3020";
type = "desktop";
platform = "x86_64-linux";
stateVersion = "24.05";
users = [ "dae" ];
};
swift3 = libx.helpers.mkHost {
hostname = "swift3";
type = "desktop";
platform = "x86_64-linux";
stateVersion = "24.05";
users = [ "dae" ];
};
rpi4 = libx.helpers.mkHost {
hostname = "rpi4";
type = "server";
platform = "aarch64-linux";
stateVersion = "24.05";
users = [ "dae" ];
};
# .iso images
# - nix build .#nixosConfigurations.iso-install.config.system.build.isoImage
iso-install = libx.helpers.mkHost {
hostname = "iso-install";
type = "iso-installer";
platform = "x86_64-linux";
stateVersion = "24.05";
users = [ "nixos" ];
};
image-sd = libx.helpers.mkHost {
hostname = "image-sd";
type = "image-sd";
platform = "aarch64-linux";
stateVersion = "24.05";
users = [ "nixos" ];
};
};
# lib/helpers.nix
mkHost =
{
hostname,
type,
platform,
stateVersion,
users,
}:
lib.nixosSystem {
system = platform;
specialArgs = {
inherit inputs users;
};
modules =
let
common = {
base = [
{
networking.hostName = hostname;
system.stateVersion = stateVersion;
users.mutableUsers = false;
}
../nixos/common/base
../hosts/${hostname}
inputs.home-manager.nixosModules.home-manager
{
home-manager = {
# makes hm use nixos's pkgs value
useGlobalPkgs = true;
# Disabling this breaks loading of profiles, https://github.com/nix-community/home-manager/issues/549. Bit odd
useUserPackages = true;
extraSpecialArgs = {
inherit inputs;
}; # allows access to flake inputs in hm modules
sharedModules = [
../home-manager/common/base
{ home.stateVersion = stateVersion; }
];
users = lib.attrsets.genAttrs users (user: {
imports = [ ../home-manager/users/${user} ];
});
};
}
] ++ builtins.map (user: ../nixos/common/users/${user}) users;
# Stuff that breaks installer and/or is not needed
base-extra = [
inputs.sops-nix.nixosModules.sops
../secrets
../nixos/common/base-extra
];
};
desktop = rec {
base = [
../nixos/desktop/base
{ home-manager.sharedModules = [ ../home-manager/desktop/base ]; }
];
auth-required = [ { home-manager.sharedModules = [ ../home-manager/desktop/auth-required ]; } ];
window-manager-common = [
../nixos/desktop/window-manager-common
{ home-manager.sharedModules = [ ../home-manager/desktop/window-manager-common ]; }
];
sway = window-manager-common ++ [
../nixos/desktop/sway
{ home-manager.sharedModules = [ ../home-manager/desktop/sway ]; }
];
cosmic = [
../nixos/desktop/cosmic
{ home-manager.sharedModules = [ ../home-manager/desktop/cosmic ]; }
];
};
server.base = [
../nixos/server/base
{ home-manager.sharedModules = [ ]; }
];
iso.installer = [
(inputs.nixpkgs + "/nixos/modules/installer/cd-dvd/installation-cd-minimal.nix")
];
image.sd = [ (inputs.nixpkgs + "/nixos/modules/installer/sd-card/sd-image-aarch64.nix") ];
in
if (type == "desktop") then
(
common.base
++ common.base-extra
++ desktop.base
++ desktop.auth-required
++ desktop.sway
++ [
{
specialisation.cosmic = {
inheritParentConfig = false;
configuration = {
imports =
common.base ++ common.base-extra ++ desktop.base ++ desktop.auth-required ++ desktop.cosmic;
};
};
}
]
)
else if (type == "server") then
(common.base ++ common.base-extra ++ server.base)
else if (type == "iso-installer") then
(common.base ++ desktop.base ++ desktop.sway ++ iso.installer)
else if (type == "image-sd") then
(common.base ++ image.sd)
else
builtins.abort "Invalid nixosConfiguration type!";
};