Hello, I’m new to NixOS and have been trying it out on an old laptop before I make the switch on my main laptop.
I have three devices I want to have similar configurations on:
- Old laptop (craptop, in my config, linked below for reference)
- Main laptop
- A PC
The old laptop and main laptop I’m going to configure alike (other than hardware and disk), but my PC has some very different requirements (mostly drivers).
I have separated my configuration into modules so I can choose which modules to include in their respective nixos/hosts/<host>/configuration.nix.
However, I’m not sure how to structure my flake.nix so I don’t just have a load of repeated code for each host, and I also specify the hostname in each hosts configuration.nix, this is a lot of repetition of the same stuff and my programming instincts tell me it is wrong and there must be a better way.
{
description = "NixOS Config";
inputs = {
nixpkgs.url = "github:nixos/nixpkgs/nixos-25.11";
nixpkgs-unstable.url = "github:nixos/nixpkgs/nixos-unstable";
home-manager = {
url = "github:nix-community/home-manager/release-25.11";
inputs.nixpkgs.follows = "nixpkgs";
};
disko = {
url = "github:nix-community/disko";
inputs.nixpkgs.follows = "nixpkgs";
};
};
outputs = {
self,
nixpkgs,
nixpkgs-unstable,
disko,
home-manager,
...
} @ inputs: let
system = "x86_64-linux";
pkgs-unstable = nixpkgs-unstable.legacyPackages.${system};
in {
nixosConfigurations.craptop = nixpkgs.lib.nixosSystem {
inherit system;
specialArgs = {
inherit inputs;
inherit pkgs-unstable;
};
modules = [
disko.nixosModules.disko
./hosts/craptop/configuration.nix
./hosts/craptop/disk-config.nix
];
};
homeConfigurations.pete = home-manager.lib.homeManagerConfiguration {
pkgs = nixpkgs.legacyPackages.${system};
modules = [../home-manager/home.nix];
};
};
}
Additionally, in my home-manager config, I set things up to use my dotfiles, but depending on the system they shouldn’t be configured. For example, on my PC with proprietary NVidia drivers, there’s no point in configuring sway when I can’t even use it. Obviously this isn’t that big of a deal because those configurations are always there, but if anyone does have a better way that combines the home-manager config with the rest of my config nicely, that would be superb.
To summarise (sorry it is a relatively long post for a relatively silly question):
- How can I configure multiple systems with the same structure but without repeating code or definitions like the hostname?
- How can I set up my
home-managerso that it isn’t completely separate from my system configuration such that it only configures software it needs to?
Any help appreciated (even if you spot something in my config that isn’t related but is objectively bad) ![]()