Originally started with: nix-starter-configs by Misterio77
Highly recommend that repository as a starting point even if it's a bit outdated.
To switch to a host configuration (e.g., "atom"):
sudo nixos-rebuild switch --flake .#atom
# or with nh, add -u to update flake inputs
nh os switch -a -H atom .This will apply the NixOS configuration defined for the "atom" host.
To switch home manager configuration for the user "matt":
home-manager switch --flake .#matt@atom
# or with nh, add -u to update flake inputs
nh home switch -a -c matt@atom .To verify any flake changes without applying them:
nix flake checkTo test a configuration without switching to it:
nixos-rebuild test --flake .#atom
# or with nh
nh os test -a -H atom .To build a configuration without applying it (will be applied on next boot):
nixos-rebuild build --flake .#atom
# or with nh
nh os build -a -H atom .Or to build vm for testing:
nixos-rebuild build-vm --flake .#atomthen run:
QEMU_OPTS="-smp 4 -m 4096 -vga virtio" ./result/bin/run-*-vm(QEMU_OPTS are for more resources)
To update all inputs in the flake:
nix flake updateTo update a specific input (e.g., nixpkgs):
nix flake update nixpkgsFor another example, to update the zed-editor input:
nix flake update zed-editor- Create a new directory under
hosts/for your host (e.g.,hosts/newhost/) - Create a
default.nixfile in that directory with your host configuration - Generate a hardware configuration with
nixos-generate-configand place it in the host directory - Add the host to the
nixosConfigurationsattribute inflake.nix:
nixosConfigurations = {
atom = nixpkgs.lib.nixosSystem { ... };
newhost = nixpkgs.lib.nixosSystem {
specialArgs = { inherit inputs outputs; };
modules = [
./hosts/newhost
];
};
};
homeConfigurations = {
"matt@atom" = home-manager.lib.homeManagerConfiguration { ... };
"newuser@newhost" = home-manager.lib.homeManagerConfiguration {
pkgs = nixpkgs.legacyPackages.x86_64-linux;
extraSpecialArgs = {inherit inputs outputs;};
modules = [
./home/newuser/newhost.nix
];
};
};- Create a new directory under
home/for your user if it doesn't exist - Create a configuration file for your user and host (e.g.,
home/newuser/newhost.nix) - Import the necessary modules and configure your home environment
- Make sure to reference your home configuration in the host's entry in
flake.nix
- System-wide settings: Edit the relevant files in the
hosts/directory - User-specific settings: Edit the relevant files in the
home/directory - Common modules: Edit or add files in the
modules/directory
- Add the relevant file in
home/<user>/programs/ - For adding a new program, create a new file and import it in
home/<user>/programs/default.nix
nixos-flake/
├── flake.nix # Main flake configuration
├── hosts/ # Host-specific configurations
│ ├── atom/ # Configuration for 'atom' host
│ └── common/ # Common configurations shared across hosts
├── home/ # Home-manager configurations
│ └── matt/ # Configuration for user 'matt'
│ ├── atom.nix
│ ├── desktop/ # Desktop-specific configurations (i3, rofi, etc.)
│ └── programs/ # Program-specific configurations
│ └── scripts/ # Scripts for user-specific tasks
└── modules/ # Custom modules
└── home-manager/ # Home-manager modules
You can format the Nix code with:
alejandra .This uses the formatter configured in flake.nix.
- This configuration uses the unstable channel of NixOS
- Several custom modules are provided for home-manager