Skip to content

Latest commit

 

History

History
330 lines (237 loc) · 7.12 KB

fresh-nixos-guide.md

File metadata and controls

330 lines (237 loc) · 7.12 KB

Guide: Setting up a git managed Flake on NixOS from scratch

This is a guide for transitioning from a fresh NixOS install to using a flake / home-manager repo like this.

https://github.com/jonwhittlestone/provisioning

Resources

We lift from the NixOS and Flakes book heavily.

https://nixos-and-flakes.thiscute.world/

Prerequisites

0. Dual boot [optional]

I wanted to dual boot with Windows so I can run Ableton.

In this case, I first installed Windows 11 iso and completed all OS updates.

https://nixos-and-flakes.thiscute.world/

And then installed NixOS with a bootable USB.

1. Fresh install -> getting essential packages

Try a sanity check build that installs vim with:

Note we set the hostName below as doylestone03. You will want to set your own host name.

# /etc/nixos/configuration.nix

...
networking.hostName = "doylestone03";
...

users.users.jon = {
  isNormalUser = true;
  description = "Jon Whittlestone";
  extraGroups = [ "networkmanager" "wheel" ];

  packages = with pkgs; [
    vim
  ];
};
sudo nano /etc/nixos/configuration.nix;
sudo nixos-rebuild switch

Start a new bash session to reflect your hostname changes

eg.

[jon@doylestone03:/etc/nixos]

2. Installing Flakes

As per the NixOS book, enable Flakes Support

# /etc/nixos/configuration.nix
{ config, pkgs, ... }:

{
  imports =
    [ # Include the results of the hardware scan.
      ./hardware-configuration.nix
    ];

  nix.settings.experimental-features = [ "nix-command" "flakes" ];

  environment.systemPackages = with pkgs; [
    # Flakes use Git to pull dependencies from data sources, so Git must be installed first
    git
    vim
    wget
    curl
  ];

...
}

And then deploy again:

sudo nixos-rebuild switch

Then create a flake.nix file:

# ensure you're in cd /etc/nixos;

sudo nix flake init -t templates#full

Add a basic example flake.nix.

All system modifications will now be managed by Flakes.

The example below has the hostname: doylestone03 and you will want to use your own hostname.

# /etc/nixos/flake.nix
{
  description = "Jon's NixOS Flake";

  inputs = {
    nixpkgs.url = "github:NixOS/nixpkgs/nixos-unstable";
    # home-manager, used for managing user configuration
    home-manager = {
      url = "github:nix-community/home-manager/release-23.05";
      inputs.nixpkgs.follows = "nixpkgs";
    };
  };

  outputs = { self, nixpkgs, ... }@inputs: {
    nixosConfigurations = {
      # deploy this configuration on any NixOS system:
      #   sudo nixos-rebuild switch --flake .#doylestone03
      "doylestone03" = nixpkgs.lib.nixosSystem {
        system = "x86_64-linux";
        modules = [
          ./configuration.nix
        ];
      };
    };
  };
}

Run: sudo nixos-rebuild swtich to deploy.

3. Installing/Using home-manager

Because NixOS can only manage system-level configuration, we need to install Home Manager to manage user-level config.

Install Home Manager as a NixOS module by creating /etc/nixos/home.nix and then adjust the params of /etc/nixos/flake.nix

# /etc/nixos/home.nix
{ config, pkgs, ... }:

{
  home.username = "jon";
  home.homeDirectory = "/home/jon";

  xresources.properties = {
    "Xcursor.size" = 16;
    "Xft.dpi" = 172;
  };

  # basic configuration of git, please change to your own
  programs.git = {
    enable = true;
    userName = "Jon Whittlestone";
    userEmail = "[email protected]";
  };

  # Packages that should be installed to the user profile.
  home.packages = with pkgs; [
    btop  # replacement of htop/nmon
  ];


  programs.bash = {
    enable = true;
    enableCompletion = true;
    bashrcExtra = ''
      export PATH="$PATH:$HOME/bin:$HOME/.local/bin:$HOME/go/bin"
    '';

    # set some aliases, feel free to add more or remove some
    shellAliases = {
      nixd = "sudo nixos-rebuild switch";
    };
  };

  home.stateVersion = "23.05";

  programs.home-manager.enable = true;
}
...

outputs = inputs@{ nixpkgs, home-manager, ... }: {
      nixosConfigurations = {
        doylestone03 = nixpkgs.lib.nixosSystem {
	  system = "x86_64-linux";
	  modules = [
	    ./configuration.nix

	    # make home-manager as a module of nixos
	    # so that home-manager configuration will be deployed automatically when executing `nixos-rebuild switch`
	    home-manager.nixosModules.home-manager
	    {
	      home-manager.useGlobalPkgs = true;
	      home-manager.useUserPackages = true;

	      home-manager.users.jon = import ./home.nix;

	      # Optionally, use home-manager.extraSpecialArgs to pass arguments to home.nix
	    }
	  ];
        };
      };
    };

4. Create symlink manage with git

As per the git section, you can place your flake in a version controlled local directory and create a symbolic link:

sudo mv /etc/nixos /etc/nixos.bak  # Backup the original configuration
sudo ln -s ~/code/provisioning/ /etc/nixos

# Deploy the flake.nix located at the default location (/etc/nixos)
sudo nixos-rebuild switch