novoid / nixos-config

NixOS + flakes + home-manager with xfce, zsh, tmux, ...

Geek Repo:Geek Repo

Github PK Tool:Github PK Tool

NixOS Configuration With Flakes, Home-manager

I wrote a long article about my journey to Nix, NixOS, Home Manager and flakes where you may find more background information about this repository and its goals.

Basic Concept

My setup is using the flake.nix as a central point.

flake.nix is calling most of the other files depending on the set hostname:

  • hosts/$HOSTNAME/default.nix
  • configuration.nix
  • all files related to home-manager: homemanager.nix and homemanager/*

The directory assets contain various configuration and binary files I wanted to keep close to my NixOS configuration files.

Wrapper Shell Scripts

I wrote several shell scripts so that I don’t have to remember the commands for updating and so forth.

you can update the setup using nixos-rebuild_switch_flake.sh which basically invokes the command:

sudo nixos-rebuild switch --flake /home/vk/nixos

In practice, I’m using nh-os-switch.sh to update my setup to meet the stored configuration. This is a visually appealing wrapper that is using nh to run:

nh os switch --ask "/home/vk/nixos"

You may need to adapt the paths to meet your path, of course.

You should start with the nixos-rebuild command and then optionally switch to nh when everything is working so far.

update-flake.sh is updating the flake definitions to the most current ones and commit them to the git repository. Therefore, as long as you don’t invoke this command, no package updates will be downloaded from the remote repositories.

From time to time, you may want to invoke garbage-collecting.sh to get rid of older data which is not required any more. configuration.nix also contains a weekly garbage collection but I’m not sure when and how this is invoked. If you need more space at a certain point in time, you’re save to invoke garbage-collecting.sh any time.

Hosts

As of 2023-12-25, I do have four different hosts running this NixOS configuration:

jackson is my business notebook (Lenovo T490) which I’m using during a business week. This is the most intensly used NixOS system at the moment by far.

floyd is my personal notebook (Lenovo X260) which I was using to test NixOS without affecting my main systems that are running Xubuntu LTS or Debian stable. I don’t work on it much. It’s mostly used for FOSS conferences I attend.

nixosvmr and nixosvms are two qemu virtual machines in order to test multi-host setup or to play with my NixOS configuration without having floyd around. Notice that as of 2023-08-09, 20GB (virtual) disk space is too small. NixOS really adds up lots of space in comparison to Debian-based distributions. I’m using the VMs rarely since I started with floyd and jackson.

Installation

  1. Download installer ISO :focus:
  2. Install NixOS using that ISO
    • Learn how to set up a USB thumb drive and boot from it for installing the basic OS
  3. Modify default configuration.nix:
    1. Start an editor:
      sudo nano /etc/nixos/configuration.nix
              
    2. Change hostname on new host from default if necessary:
    3. Activate flakes: https://nixos.wiki/wiki/Flakes “On NixOS set the following options in configuration.nix and run nixos-rebuild
      { pkgs, ... }: {   # usual, this line is already there
        
        nix.settings.experimental-features = [ "nix-command" "flakes" ];
        
      }                  # usual, this line is already there
              
    4. Add the git and package. Also add your favourite editor as well for fixing miscellaneous things. I’m using vim for that.
    5. OPTIONALLY: Install packages like vim, sshd, flakes in case you need to play around more
    6. Update and activate your setup
      sudo nixos-rebuild switch
              
  4. Get this NixOS configuration git repository via (scp or git clone https://github.com/novoid/nixos-config.git ~/nixos)
    • target directory: ~/nixos/nixos-configuration
      • I’m using following command at my LAN:
        scp -r vk@192.168.2.42:src/nixos-configuration ~/ && mv ~/nixos-configuration ~/nixos
                    
  5. Create a configuration for the $HOSTNAME in the nixos-configuration git repo
    1. Adapt flakes.nix
    2. Create ~/nixos/hosts/$HOSTNAME (from an existing host as template)
    3. Get the hardware-configuration.nix into your new setup
      cd ~/nixos
      cp /etc/nixos/hardware-configuration.nix ~/nixos/hosts/$HOSTNAME/
      cp /etc/nixos/configuration.nix ~/nixos/hosts/$HOSTNAME/default.nix
      git add 
              
  6. Check for things that won’t work or need attention when you’re not Karl Voit:
    • Check out all spots marked with “SPECIFICTOKARL” for things that are different on your side:
      git grep -B 1 -A 2 SPECIFICTOKARL
              
      • flake.nix: description, system.stateVersion, username “vk”, …
    • Check out all spots marked with “FIXXME” for things that may need attention to fix:
      git grep -B 1 -A 2 FIXXME
              
    • Check out all spots marked with “DEPENDENCY” for things that are referring to stuff which is not available in this NixOS repository:
      git grep -B 1 -A 2 DEPENDENCY
              
      • Usually, you might want to get rid of those things because the would result in errors in your situation.
  7. Run flakes with the new setup:
    1. Switch to nixos-config dir
    2. Double-check: make sure that hostname matches a config
    3. Run flakes with current nixos-config dir:
      sudo nixos-rebuild switch --flake .
              
    4. Fix any error that may arise at this point.
      • Yes, this can be frustrating. I wish you good luck here.
      • Usually, you just have to remove stuff from ~/nixos/hosts/$HOSTNAME/default.nix when it is also defined in flakes.nix or other existing files.
      • Futhermore, you might have to create (and git add) configuration files that are assumed to exist such as assets/.tmuxp/$HOSTNAME.yaml.
    5. I do recommend a reboot here to cleanly boot the updated OS with all of its settings.
    6. Execute store optimization (replacing copies with hardlinks):
      nix-store --optimise
              
    7. OPTIONALLY: Symlink /etc/nixos to ~/nixos if you want to use nixos-rebuild without the optional parameter for the path.

Setup GitHub ssh authentication

This is quite handy to be included here because this is what I typically do right after setting up NixOS if I don’t take over the old keys via my previous dotfiles.


Please do replace $HOST (your hostname), $USER (your local username) and $GITHUBUSER (username at GitHub) accordingly:

  1. execute
    ssh-keygen -t ed25519 -C "git.$HOST@karl-voit.at"
        
    • This creates a new SSH key, using the provided email as a label.
  2. At the prompt, type a secure passphrase.
  3. Choose a file name:
    /home/$USER/.ssh/id_ed25519_Git_$HOST
        
  4. adding to ssh-agend:
    ssh-add ~/.ssh/id_ed25519_Git_$HOST
        
  5. visit https://github.com/settings/keys
  6. click on “New SSH key”
  7. insert a name and paste the content of the following file:
    ~/.ssh/id_ed25519_Git_$HOST.pub
        
  8. add the following lines to your ~/.ssh/config file (create if it doesn’t exist):
    host github.com
        IdentityFile ~/.ssh/id_ed25519_Git_$HOST
        User $GITHUBUSER
        

License

If not specified otherwise, this configuration is licensed under a Creative Commons Attribution-ShareAlike 4.0 International License

About

NixOS + flakes + home-manager with xfce, zsh, tmux, ...

License:Other


Languages

Language:Nix 99.5%Language:Shell 0.5%Language:CSS 0.1%