Skip to content

MAHDTech/nix-config

Repository files navigation

Nix config

These are my dotfiles, there are many like them but these ones are mine.

  • Dotfiles Creed

Table of Contents

Overview

Nix logo

Check Update

These are my dotfiles and system configurations managed as a Nix flake.

The idea behind the configuration layout is split into a few parts;

  • Home configuration is managed using Home Manager under home.
  • Host configuration contains unique items for individual hosts.
  • System configuration bundles common system services and programs under system.

⚠️ Warning

🐉 Here be dragons 🐉

The author is still on their journey to declarative enlightenment with Nix, so don't assume they know wtf they are doing or that this repo resembles best practice in any way, shape or form.

EDIT: Multiple years on using Nix and I still feel like I'm learning new things every day.

Why?

After managing thousands of servers with CAPS tooling like Ansible and the Salt Project I longed for a declarative, immutable and single-source of truth configuration framework.

Then I found Nix and NixOS.

Nix might not be perfect, but it's a hell of a lot better than the brittle, hacked together shell scripts that I have left behind.

NixOS might have a steep learning curve, but it's been worth it imo.

NixOS Learning Curve

Layout

Folders

Name Description
home-manager/* Home configuration using Home Manager
nixos/hosts Host specific configuration
nixos/system System configurations using Nix
scripts Scripts not managed with Nix

Setup

Bootstrap

  • Clone the repo
export NIX_CONFIG_REPO="https://github.com/MAHDTech/nix-config.git"

git clone ${NIX_CONFIG_REPO} "$/HOME/dotfiles"
cd "${HOME}/dotfiles"
  • Review and run the script below.

NOTE: This script (so far) only intended to configure either;

  • A ZFS on root install on NixOS (NixOS & Home Manager)
  • A ChromeOS Debian Linux container (Home Manager only)
  • A WSL setup on Windows 11 (Home Manager only)

# Review and modify the defined variables as required.
vim ./scripts/bootstrap.sh

# Run the script
./scripts/bootstrap.sh

Manual (NixOS)

If you want to instead perform the setup manually;

  • Make sure flakes are enabled.
nix = {
  package = pkgs.nixFlakes;
  extraOptions = ''
    experimental-features = nix-command flakes
  '';
};
  • Apply if necessary
sudo nixos-rebuild switch
  • Apply the desired host configuration
export NIXPKGS_ALLOW_UNFREE=1

NIXOS_HOST="NUC"

nixos-rebuild \
    boot  \
    --sudo \
    --upgrade-all \
    --refresh \
    --impure \
    --flake ".#${NIXOS_HOST}"
  • Check the status of the home-manager systemd unit
systemctl status "home-manager-$USER.service"

Usage

NixOS

NixOS changes are applied on each boot.

nixos-rebuild \
    boot  \
    --sudo \
    --upgrade-all \
    --refresh \
    --impure \
    --flake '.#'

Or remotely with;

nixos-rebuild \
    boot  \
    --sudo \
    --upgrade-all \
    --refresh \
    --flake 'github:MAHDTech/nix-config#'

Home Manager

Home Manager changes are switched over with;

home-manager \
    switch
    --flake .
  • Or remotely with;
home-manager \
    switch
    --flake 'github:MAHDTech/nix-config'

Updates

Updating the Nix flake lock file flake.lock is done via GitHub Actions.

The manual method is to run the following command within the root of the repository;

nix flake update

YOLO

The YOLO method for Crostini/WSL;

PROJECTS="$HOME/Projects"
NIX_CONFIG_REPO="[email protected]:MAHDTech/nix-config.git"
NIX_CONFIG_DIR="$HOME/dotfiles"

sudo apt update
sudo apt install --yes \
    git \
    curl \
    wget \
    mesa-utils \
    intel-gpu-tools \
    gnome-keyring

# TODO: Get gnome-keyring working correctlyin home-manger.

ssh-keygen -t ed25519
cat ~/.ssh/id_ed25519.pub
read -rp "Now add temporary SSH key to GitHub..." TEMP

mkdir -p "${PROJECTS}"
git clone ${NIX_CONFIG_REPO} "${NIX_CONFIG_DIR}"
pushd "${NIX_CONFIG_DIR}"

./scripts/bootstrap.sh

# Crostini cleanup just the temp key
rm ~/.ssh/id_ed25519*

# For WSL, cleanup all SSH as ssh.exe is used via aliases.
rm -rf ~/.ssh

Generating ISO images

# x86-64
nixos-generate --show-trace --flake .#NAME --format install-iso --out-link result --system x86_64-linux

# aarch64
nixos-generate --show-trace --flake .#NAME --format install-iso --out-link result --system aarch64-linux

Troubleshooting

Some notes on recovering the system when things go bad...

  1. Boot the NixOS Live ISO.

  2. Mount the ZFS datasets to a temporary altroot.

sudo mkdir /mnt/nixos

sudo zpool import -f -R /mnt/nixos zpool
  1. Enter the broken NixOS chroot
nixos-enter --root /mnt/nixos
  1. Re-run the flake after fixing it
nixos-rebuild \
    boot  \
    --sudo \
    --upgrade-all \
    --refresh \
    --flake 'github:MAHDTech/nix-config#'

About

Nix configuration dotfiles

Topics

Resources

License

Stars

Watchers

Forks

Contributors 3

  •  
  •  
  •