From b43660c2275fc74e42b4de4700eac974cd98a37e Mon Sep 17 00:00:00 2001
From: Jo <johannesreckers2006@gmail.com>
Date: Fri, 6 Sep 2024 17:43:30 +0200
Subject: [PATCH] refactor: split systems into archetypes

refactor(module): move kernel config to module

refactor(module): move nix config to module

refactor(module): move networking config to module

refactor(module): move nix config to module

refactor(module): move grub config to module
---
 .../x86_64-linux/jo@puzzlevision/default.nix  | 17 ++++-
 modules/home/apps/development/zed-editor.nix  |  2 -
 .../nixos/archetypes/workstation/default.nix  | 40 ++++++++++++
 modules/nixos/common/bluetooth/default.nix    | 37 +++++++++++
 modules/nixos/common/grub/default.nix         | 38 +++++++++++
 modules/nixos/common/kernel/default.nix       | 32 ++++++++++
 modules/nixos/common/networking/default.nix   | 24 +++++++
 modules/nixos/common/nix/default.nix          | 40 ++++++++++++
 systems/x86_64-linux/puzzlevision/default.nix | 64 +------------------
 9 files changed, 228 insertions(+), 66 deletions(-)
 create mode 100644 modules/nixos/archetypes/workstation/default.nix
 create mode 100644 modules/nixos/common/bluetooth/default.nix
 create mode 100644 modules/nixos/common/grub/default.nix
 create mode 100644 modules/nixos/common/kernel/default.nix
 create mode 100644 modules/nixos/common/networking/default.nix
 create mode 100644 modules/nixos/common/nix/default.nix

diff --git a/homes/x86_64-linux/jo@puzzlevision/default.nix b/homes/x86_64-linux/jo@puzzlevision/default.nix
index 6c537b5..3b43334 100644
--- a/homes/x86_64-linux/jo@puzzlevision/default.nix
+++ b/homes/x86_64-linux/jo@puzzlevision/default.nix
@@ -18,7 +18,18 @@
   # All other arguments come from the home home.
   config,
   ...
-}: with lib; with lib.${namespace}; {
+}: with lib; with lib.${namespace};
+let
+    zed-fhs = pkgs.buildFHSUserEnv {
+        name = "zed";
+        targetPkgs = pkgs:
+        with pkgs; [
+            zed-editor
+        ];
+        runScript = "zed";
+    };
+in
+{
   imports = [
     ./apps/gnome.nix
   ];
@@ -54,7 +65,7 @@
     nodejs_22
     bun
     devenv
-    zed-editor
+    zed-fhs
 
     ### Rust development specific
     rustup
@@ -71,7 +82,7 @@
     sidequest
   ];
 
-  puzzlevision.apps.zed-editor.enable = true;
+  #lib.puzzlevision.apps.zed-editor.enable = true;
 
   home.stateVersion = "24.05";
 }
diff --git a/modules/home/apps/development/zed-editor.nix b/modules/home/apps/development/zed-editor.nix
index 1b97b6d..c9669b6 100644
--- a/modules/home/apps/development/zed-editor.nix
+++ b/modules/home/apps/development/zed-editor.nix
@@ -27,8 +27,6 @@ let
 in {
     options.${namespace}.apps.zed-editor = { enable = mkEnableOption "zed-editor"; };
 
-    home.packages = [zed-fhs];
-
     config = mkIf cfg.enable {
         home.packages = [zed-fhs];
     };
diff --git a/modules/nixos/archetypes/workstation/default.nix b/modules/nixos/archetypes/workstation/default.nix
new file mode 100644
index 0000000..596f7bc
--- /dev/null
+++ b/modules/nixos/archetypes/workstation/default.nix
@@ -0,0 +1,40 @@
+{
+  lib,
+  pkgs,
+  inputs,
+
+  namespace, # The flake namespace, set in flake.nix. If not set, defaults to "internal".
+  system, # The system architecture for this host (eg. `x86_64-linux`).
+  target, # The Snowfall Lib target for this system (eg. `x86_64-iso`).
+  format, # A normalized name for the system target (eg. `iso`).
+  virtual, # A boolean to determine whether this system is a virtual target using nixos-generators.
+  systems, # An attribute map of your defined hosts.
+
+  config,
+  ...
+}: with lib; with lib.${namespace};
+let
+  cfg = config.${namespace}.archetypes.workstation;
+in {
+  options.${namespace}.archetypes.workstation = { enable = mkEnableOption "Enable the workstation archetype for your current system"; };
+
+  config = mkIf cfg.enable {
+    environment.sessionVariables = {
+      NIXOS_OZONE_WL = "1"; # Chromium/Electron native Wayland support
+      MOZ_ENABLE_WAYLAND = "1"; # Firefox native Wayland support
+    };
+
+    # Enable modules
+    puzzlevision = {
+      common = {
+        nix.enable = true; # Standard Nix configuration
+        grub.enable = true; # Bootloader grub
+        networking.enable = true; # Networkmanager configuration
+        kernel.enable = true; # Kernel modifications
+        bluetooth.enable = true; # Bluetooth support
+      };
+
+      desktop.gnome.enable = true;
+    };
+  };
+}
diff --git a/modules/nixos/common/bluetooth/default.nix b/modules/nixos/common/bluetooth/default.nix
new file mode 100644
index 0000000..f60a7a3
--- /dev/null
+++ b/modules/nixos/common/bluetooth/default.nix
@@ -0,0 +1,37 @@
+{
+  lib,
+  pkgs,
+  inputs,
+
+  namespace, # The flake namespace, set in flake.nix. If not set, defaults to "internal".
+  system, # The system architecture for this host (eg. `x86_64-linux`).
+  target, # The Snowfall Lib target for this system (eg. `x86_64-iso`).
+  format, # A normalized name for the system target (eg. `iso`).
+  virtual, # A boolean to determine whether this system is a virtual target using nixos-generators.
+  systems, # An attribute map of your defined hosts.
+
+  config,
+  ...
+}: with lib; with lib.${namespace};
+let
+  cfg = config.${namespace}.common.bluetooth;
+in {
+  options.${namespace}.common.bluetooth = { enable = mkEnableOption "Enable bluetooth support on your current system"; };
+
+  config = mkIf cfg.enable {
+    hardware.bluetooth = {
+      enable = true;
+      powerOnBoot = true;
+      package = pkgs.bluez;
+
+      settings = {
+        General = {
+          ControllerMode = "dual";
+          FastConnectable = "true";
+          Experimental = "true";
+          KernelExperimental = "true";
+        };
+      };
+    };
+  }
+}
diff --git a/modules/nixos/common/grub/default.nix b/modules/nixos/common/grub/default.nix
new file mode 100644
index 0000000..7c811c9
--- /dev/null
+++ b/modules/nixos/common/grub/default.nix
@@ -0,0 +1,38 @@
+{
+  lib,
+  pkgs,
+  inputs,
+
+  namespace, # The flake namespace, set in flake.nix. If not set, defaults to "internal".
+  system, # The system architecture for this host (eg. `x86_64-linux`).
+  target, # The Snowfall Lib target for this system (eg. `x86_64-iso`).
+  format, # A normalized name for the system target (eg. `iso`).
+  virtual, # A boolean to determine whether this system is a virtual target using nixos-generators.
+  systems, # An attribute map of your defined hosts.
+
+  config,
+  ...
+}: with lib; with lib.${namespace};
+let
+  cfg = config.${namespace}.common.grub;
+in {
+  options.${namespace}.common.grub = { enable = mkEnableOption "grub"; };
+
+  config = mkIf cfg.enable {
+    boot.loader.grub = {
+      enable = true;
+      devices = [ "nodev" ];
+      efiInstallAsRemovable = true;
+      efiSupport = true;
+
+      extraEntries = ''
+        menuentry "Reboot" {
+          reboot
+        }
+        menuentry "Poweroff" {
+          halt
+        }
+      '';
+    };
+  }
+}
diff --git a/modules/nixos/common/kernel/default.nix b/modules/nixos/common/kernel/default.nix
new file mode 100644
index 0000000..b7ac142
--- /dev/null
+++ b/modules/nixos/common/kernel/default.nix
@@ -0,0 +1,32 @@
+{
+  lib,
+  pkgs,
+  inputs,
+
+  namespace, # The flake namespace, set in flake.nix. If not set, defaults to "internal".
+  system, # The system architecture for this host (eg. `x86_64-linux`).
+  target, # The Snowfall Lib target for this system (eg. `x86_64-iso`).
+  format, # A normalized name for the system target (eg. `iso`).
+  virtual, # A boolean to determine whether this system is a virtual target using nixos-generators.
+  systems, # An attribute map of your defined hosts.
+
+  config,
+  ...
+}: with lib; with lib.${namespace};
+let
+  cfg = config.${namespace}.common.kernel;
+in {
+  options.${namespace}.common.kernel = {
+    enable = mkEnableOption "Modify the standard kernel settings";
+    version = mkOption {
+      type = lib.types.str;
+      default = "latest";
+      example = "latest";
+      description = "Set the kernel version to be used by your system"
+    };
+  };
+
+  config = mkIf cfg.enable {
+    kernelPackages = pkgs.linuxPackages_${cfg.version};
+  };
+}
diff --git a/modules/nixos/common/networking/default.nix b/modules/nixos/common/networking/default.nix
new file mode 100644
index 0000000..2c00fb8
--- /dev/null
+++ b/modules/nixos/common/networking/default.nix
@@ -0,0 +1,24 @@
+{
+  lib,
+  pkgs,
+  inputs,
+
+  namespace, # The flake namespace, set in flake.nix. If not set, defaults to "internal".
+  system, # The system architecture for this host (eg. `x86_64-linux`).
+  target, # The Snowfall Lib target for this system (eg. `x86_64-iso`).
+  format, # A normalized name for the system target (eg. `iso`).
+  virtual, # A boolean to determine whether this system is a virtual target using nixos-generators.
+  systems, # An attribute map of your defined hosts.
+
+  config,
+  ...
+}: with lib; with lib.${namespace};
+let
+  cfg = config.${namespace}.common.networking;
+in {
+  options.${namespace}.common.networking = { enable = mkEnableOption "networking"; };
+
+  config = mkIf cfg.enable {
+    networking.networkmanager.enable = true;
+  };
+}
diff --git a/modules/nixos/common/nix/default.nix b/modules/nixos/common/nix/default.nix
new file mode 100644
index 0000000..c32ed9e
--- /dev/null
+++ b/modules/nixos/common/nix/default.nix
@@ -0,0 +1,40 @@
+{
+  lib,
+  pkgs,
+  inputs,
+
+  namespace, # The flake namespace, set in flake.nix. If not set, defaults to "internal".
+  system, # The system architecture for this host (eg. `x86_64-linux`).
+  target, # The Snowfall Lib target for this system (eg. `x86_64-iso`).
+  format, # A normalized name for the system target (eg. `iso`).
+  virtual, # A boolean to determine whether this system is a virtual target using nixos-generators.
+  systems, # An attribute map of your defined hosts.
+
+  config,
+  ...
+}: with lib; with lib.${namespace};
+let
+  cfg = config.${namespace}.common.nix;
+in {
+  options.${namespace}.common.nix = { enable = mkEnableOption "nix"; };
+
+  config = mkIf cfg.enable {
+    nix = {
+      settings = {
+        auto-optimise-store = true;
+        builders-use-substitutes = true;
+        experimental-features = [ "nix-command" "flakes" ];
+        keep-derivations = true;
+        keep-outputs = true;
+        max-jobs = "auto";
+      };
+
+      # Garbage collection configuration.
+      gc = {
+        automatic = true;
+        dates = "daily";
+        options = "--delete-older-than 3d";
+      };
+    };
+  }
+}
diff --git a/systems/x86_64-linux/puzzlevision/default.nix b/systems/x86_64-linux/puzzlevision/default.nix
index a94795a..c2aa4a8 100644
--- a/systems/x86_64-linux/puzzlevision/default.nix
+++ b/systems/x86_64-linux/puzzlevision/default.nix
@@ -29,35 +29,11 @@ with lib.${namespace};
     inputs.hardware.nixosModules.common-pc-laptop-ssd
   ];
 
-  nix = {
-    settings = {
-      auto-optimise-store = true;
-      builders-use-substitutes = true;
-      experimental-features = [ "nix-command" "flakes" ];
-      keep-derivations = true;
-      keep-outputs = true;
-      max-jobs = "auto";
-      warn-dirty = false;
-    };
-
-    # Garbage collection configuration.
-    gc = {
-      automatic = true;
-      dates = "daily";
-      options = "--delete-older-than 3d";
-    };
-  };
-
   # Set hostname
+  # Todo: move to common/networking module
   networking.hostName = "puzzlevision";
 
-  # Enable networking through networkmanager (required for most desktop environments).
-  networking.networkmanager.enable = true;
-
   boot = {
-    # Always run the latest kernel.
-    kernelPackages = pkgs.linuxPackages_latest;
-
     # Configure additional kernel modules.
     extraModulePackages = [
       pkgs.linuxPackages_latest.rtl8821ce # Use custom network-card driver.
@@ -66,23 +42,6 @@ with lib.${namespace};
     blacklistedKernelModules = [
       "rtw88_8821ce" # Block the default network-card driver.
     ];
-
-    # Grub configuration.
-    loader.grub = {
-      enable = true;
-      devices = [ "nodev" ];
-      efiInstallAsRemovable = true;
-      efiSupport = true;
-
-      extraEntries = ''
-        menuentry "Reboot" {
-          reboot
-        }
-        menuentry "Poweroff" {
-          halt
-        }
-      '';
-    };
   };
 
   # Set timezone.
@@ -127,24 +86,8 @@ with lib.${namespace};
     pulse.enable = true;
   };
 
-  # Bluetooth configuration.
-  hardware.bluetooth = {
-    enable = true;
-    powerOnBoot = true;
-    package = pkgs.bluez;
-
-    settings = {
-      General = {
-        ControllerMode = "dual";
-        FastConnectable = "true";
-        Experimental = "true";
-        KernelExperimental = "true";
-      };
-    };
-  };
-
-  # Enable Gnome
-  puzzlevision.desktop.gnome.enable = true;
+  # Set system Type
+  puzzlevision.archetypes.workstation.enable = true;
 
   # Enable flatpak support.
   services.flatpak.enable = true;
@@ -177,7 +120,6 @@ with lib.${namespace};
     nano
     firefox
     chromium
-    lutris
     vlc
     spotify