From 80d4bb746e06482f9a8239cf973535837699fce8 Mon Sep 17 00:00:00 2001
From: Felix Tenley <dev@felschr.com>
Date: Wed, 23 Sep 2020 10:36:46 +0200
Subject: [PATCH] feat: migrate to nix flake

---
 README.md                | 45 +++++++++++++++++++++++++----
 flake.lock               | 62 ++++++++++++++++++++++++++++++++++++++++
 flake.nix                | 60 ++++++++++++++++++++++++++++++++++++++
 hardware/felix-nixos.nix | 43 ++++++++++++++++++++++++++++
 home-pc.nix              |  4 ---
 home/git.nix             |  1 -
 home/signal.nix          |  6 ++--
 install.sh               | 29 -------------------
 system/default.nix       |  1 -
 system/nix.nix           |  6 ++++
 system/nur.nix           |  9 ------
 work-pc.nix              |  4 ---
 12 files changed, 214 insertions(+), 56 deletions(-)
 create mode 100644 flake.lock
 create mode 100644 flake.nix
 create mode 100644 hardware/felix-nixos.nix
 delete mode 100755 install.sh
 delete mode 100644 system/nur.nix

diff --git a/README.md b/README.md
index c87c20a..a0e1825 100644
--- a/README.md
+++ b/README.md
@@ -1,8 +1,43 @@
-# FelschR's NixOS configuration
+# felschr's NixOS configuration
 
-## Installation on new machine
-To setup a new machine run the following command after completing partitioning and mounting:
+## Installation
+
+Clone the configuraiton into `etc/nixos`.
+
+On a new machine run:
+
+```sh
+nixos-generate-config --root /mnt
 ```
-./install.sh <NIX_CONFIG>
+
+Then move the resulting `/etc/nixos/hardware-configuration.nix` to `./hardware/<config>.nix` and adjust it and the `flake.nix` accodringly.
+Make sure everything was properly recognised. Btrfs mount options might be missing, for example.
+
+To install run the following command where `<config>` matches `outputs.nixosConfigurations.<config>` in `flake.nix`:
+
+```sh
+nixos-install --flake /etc/nixos#<config>
+```
+
+## Updating
+
+Update all or specific locked flake inputs:
+
+```sh
+nix flake update
+nix flake update --update-input <input>
+```
+
+## Rebuilding the system
+
+Rebuild the system:
+
+```sh
+sudo nixos-rebuild switch
+```
+
+Update flake.lock and rebuild the system:
+
+```sh
+nix flake update && sudo nixos-rebuild switch
 ```
-This runs `nixos-generate-config`, symlinks the passed configuration to `/etc/nixos/configuration.nix`, sets up required nix channels and then runs `nixos-install`.
diff --git a/flake.lock b/flake.lock
new file mode 100644
index 0000000..9b50bbb
--- /dev/null
+++ b/flake.lock
@@ -0,0 +1,62 @@
+{
+  "nodes": {
+    "home-manager": {
+      "flake": false,
+      "locked": {
+        "lastModified": 1600810013,
+        "narHash": "sha256-Zcvg0/ot+EfV1fOdUF0G/GY2zBq76ksv92cpa2E2xvk=",
+        "owner": "nix-community",
+        "repo": "home-manager",
+        "rev": "a6a3abb295777b1d3ac1ca7f2d47bd0daf7f9638",
+        "type": "github"
+      },
+      "original": {
+        "owner": "nix-community",
+        "ref": "master",
+        "repo": "home-manager",
+        "type": "github"
+      }
+    },
+    "nixpkgs": {
+      "locked": {
+        "lastModified": 1600573085,
+        "narHash": "sha256-w5SGosyuTbFCBoUzgy2gyVcxYxRUvZ6SgHsRIPkZXgI=",
+        "owner": "NixOS",
+        "repo": "nixpkgs",
+        "rev": "1179840f9a88b8a548f4b11d1a03aa25a790c379",
+        "type": "github"
+      },
+      "original": {
+        "owner": "NixOS",
+        "ref": "nixos-unstable",
+        "repo": "nixpkgs",
+        "type": "github"
+      }
+    },
+    "nur": {
+      "locked": {
+        "lastModified": 1600834397,
+        "narHash": "sha256-LGv1Red7btJ4fyiYjeVOhThJ5mulFgV7E9ZQJImg1To=",
+        "owner": "nix-community",
+        "repo": "NUR",
+        "rev": "b347177e8aeac5eb21b0fd58966c24dee754039d",
+        "type": "github"
+      },
+      "original": {
+        "owner": "nix-community",
+        "ref": "master",
+        "repo": "NUR",
+        "type": "github"
+      }
+    },
+    "root": {
+      "inputs": {
+        "home-manager": "home-manager",
+        "nixpkgs": "nixpkgs",
+        "nur": "nur"
+      }
+    }
+  },
+  "root": "root",
+  "version": 7
+}
diff --git a/flake.nix b/flake.nix
new file mode 100644
index 0000000..df49ec2
--- /dev/null
+++ b/flake.nix
@@ -0,0 +1,60 @@
+{
+  inputs.nixpkgs.url = "github:NixOS/nixpkgs/nixos-unstable";
+
+  inputs.home-manager = {
+    url = "github:nix-community/home-manager/master";
+    flake = false;
+  };
+
+  inputs.nur.url = "github:nix-community/NUR/master";
+
+  outputs = { self, nixpkgs, home-manager, nur }: let
+    systemModule = { hostName, hardwareConfig, config }: ({ pkgs, ... }: {
+      networking.hostName = hostName;
+
+      # Let 'nixos-version --json' know about the Git revision
+      # of this flake.
+      system.configurationRevision = nixpkgs.lib.mkIf (self ? rev) self.rev;
+
+      nix.registry.nixpkgs.flake = nixpkgs;
+
+      nixpkgs.overlays = [
+        nur.overlay
+      ];
+
+      imports = [
+        hardwareConfig
+        "${home-manager}/nixos"
+        config
+      ];
+    });
+  in {
+
+    nixosConfigurations.felix-nixos = nixpkgs.lib.nixosSystem {
+      system = "x86_64-linux";
+      modules =
+        [
+          nixpkgs.nixosModules.notDetected
+          (systemModule {
+            hostName = "felix-nixos";
+            hardwareConfig = ./hardware/felix-nixos.nix;
+            config = ./home-pc.nix;
+          })
+        ];
+    };
+
+    nixosConfigurations.pilot1 = nixpkgs.lib.nixosSystem {
+      system = "x86_64-linux";
+      modules =
+        [
+          nixpkgs.nixosModules.notDetected
+          (systemModule {
+            hostName = "pilot1";
+            hardwareConfig = ./hardware-configuration.nix; # TODO
+            config = ./work-pc.nix;
+          })
+        ];
+    };
+
+  };
+}
diff --git a/hardware/felix-nixos.nix b/hardware/felix-nixos.nix
new file mode 100644
index 0000000..081a985
--- /dev/null
+++ b/hardware/felix-nixos.nix
@@ -0,0 +1,43 @@
+{ config, lib, pkgs, ... }:
+
+{
+  boot.initrd.availableKernelModules = [ "nvme" "xhci_pci" "ahci" "usbhid" "usb_storage" "sd_mod" "sr_mod" ];
+  boot.initrd.kernelModules = [ ];
+  boot.kernelModules = [ "kvm-amd" ];
+  boot.extraModulePackages = [ ];
+
+  fileSystems."/" =
+    { device = "/dev/disk/by-uuid/9ef41d63-a7ad-406d-8c2b-5ad3fb4c0ea6";
+      fsType = "btrfs";
+      options = [ "subvol=@" "compress-force=zstd" "noatime" ];
+    };
+
+  boot.initrd.luks.devices."enc".device = "/dev/disk/by-uuid/6f4f3ce1-57fd-4ec3-bb9d-7847853d2dcf";
+
+  fileSystems."/home" =
+    { device = "/dev/disk/by-uuid/9ef41d63-a7ad-406d-8c2b-5ad3fb4c0ea6";
+      fsType = "btrfs";
+      options = [ "subvol=@home" "compress-force=zstd" "noatime" ];
+    };
+
+  fileSystems."/swap" =
+    { device = "/dev/disk/by-uuid/9ef41d63-a7ad-406d-8c2b-5ad3fb4c0ea6";
+      fsType = "btrfs";
+      options = [ "subvol=@swap" ];
+      neededForBoot = true;
+    };
+
+  fileSystems."/.snapshots" =
+    { device = "/dev/disk/by-uuid/9ef41d63-a7ad-406d-8c2b-5ad3fb4c0ea6";
+      fsType = "btrfs";
+      options = [ "subvol=@snapshots" "compress-force=zstd" "noatime" ];
+    };
+
+  fileSystems."/boot" =
+    { device = "/dev/disk/by-uuid/5C20-4516";
+      fsType = "vfat";
+    };
+
+  swapDevices = [ ];
+
+}
diff --git a/home-pc.nix b/home-pc.nix
index c6ba03a..2fafa3c 100644
--- a/home-pc.nix
+++ b/home-pc.nix
@@ -2,8 +2,6 @@
 
 {
   imports = [
-    ./hardware-configuration.nix
-    <home-manager/nixos>
     ./hardware/base.nix
     ./hardware/gpu-nvidia.nix
     ./hardware/ledger.nix
@@ -21,8 +19,6 @@
     "p7zip-16.02" # currently used by lutris
   ];
 
-  networking.hostName = "felix-nixos";
-
   hardware.enableAllFirmware = true;
   hardware.cpu.amd.updateMicrocode = true;
   services.fwupd.enable = true;
diff --git a/home/git.nix b/home/git.nix
index 77e0694..0d2271e 100644
--- a/home/git.nix
+++ b/home/git.nix
@@ -21,7 +21,6 @@
         dirs       = [ "~/dev/work/" ];
       };
     };
-    defaultProfile = "private";
 
     ignores = [".direnv"];
     signing = {
diff --git a/home/signal.nix b/home/signal.nix
index 6443a44..101390f 100644
--- a/home/signal.nix
+++ b/home/signal.nix
@@ -2,8 +2,6 @@
 
 with pkgs;
 let
-  # TODO this doesn't affect the desktop file
-  # e.g. when starting via GNOME the flag is not set
   signal-desktop = runCommand "signal-desktop" {
     buildInputs = [ makeWrapper ];
   } ''
@@ -17,8 +15,10 @@ in
 {
   home.packages = [ signal-desktop ];
 
+  # TODO switch to overwritten `signal-desktop` when
+  # desktop file is updated with correct exec path
   xdg.configFile."autostart/signal-desktop.desktop".text =
     builtins.replaceStrings
       ["bin/signal-desktop"] ["bin/signal-desktop --start-in-tray"]
-      (builtins.readFile "${signal-desktop}/share/applications/signal-desktop.desktop");
+      (builtins.readFile "${pkgs.signal-desktop}/share/applications/signal-desktop.desktop");
 }
diff --git a/install.sh b/install.sh
deleted file mode 100755
index 9149338..0000000
--- a/install.sh
+++ /dev/null
@@ -1,29 +0,0 @@
-#!/bin/sh
-
-set -e
-
-if [ $EUID != 0 ]; then
-    sudo "$0" "$@"
-    exit $?
-fi
-
-CONFIG=$1
-
-if [ -z "$CONFIG" ]
-then
-  echo "path to config to use as configuration.nix needs to be passed as first argument"
-  exit 1
-else
-  echo "using configuration: '$CONFIG'"
-fi
-
-ln -s $CONFIG configuration.nix
-
-nixos-generate-config --root /mnt
-
-# add nixos-unstable and home-manager channels
-nix-channel --add https://nixos.org/channels/nixos-unstable nixos
-nix-channel --add https://github.com/nix-community/home-manager/archive/master.tar.gz home-manager
-nix-channel --update
-
-nixos-install
diff --git a/system/default.nix b/system/default.nix
index f2737c8..2d30faa 100644
--- a/system/default.nix
+++ b/system/default.nix
@@ -5,7 +5,6 @@
     ./hardened.nix
     ./i18n.nix
     ./nix.nix
-    ./nur.nix
     ./vpn.nix
   ];
 
diff --git a/system/nix.nix b/system/nix.nix
index e1f4902..ccbe528 100644
--- a/system/nix.nix
+++ b/system/nix.nix
@@ -1,6 +1,12 @@
 { config, pkgs, ... }:
 
 {
+  # for flakes support
+  nix.package = pkgs.nixUnstable;
+  nix.extraOptions = ''
+    experimental-features = nix-command flakes
+  '';
+
   nix.autoOptimiseStore = true;
   nix.gc = {
     automatic = true;
diff --git a/system/nur.nix b/system/nur.nix
deleted file mode 100644
index c0eaf61..0000000
--- a/system/nur.nix
+++ /dev/null
@@ -1,9 +0,0 @@
-{ config, pkgs, ... }:
-
-{
-  nixpkgs.config.packageOverrides = pkgs: {
-    nur = import (builtins.fetchTarball "https://github.com/nix-community/NUR/archive/master.tar.gz") {
-      inherit pkgs;
-    };
-  };
-}
diff --git a/work-pc.nix b/work-pc.nix
index 17880c0..2a6d58a 100644
--- a/work-pc.nix
+++ b/work-pc.nix
@@ -2,8 +2,6 @@
 
 {
   imports = [
-    ./hardware-configuration.nix
-    <home-manager/nixos>
     ./hardware/base.nix
     ./hardware/gpu-bumblebee.nix
     ./system
@@ -21,8 +19,6 @@
 
   nixpkgs.config.allowUnfree = true;
 
-  networking.hostName = "pilot1-nixos"; # Define your hostname.
-
   hardware.enableAllFirmware = true;
   hardware.cpu.intel.updateMicrocode = true;
   hardware.logitech.enable = true;