From 1525120e295fa9cad4d07008faf6d6c9aa0bcb3d Mon Sep 17 00:00:00 2001
From: coolneng <akasroua@gmail.com>
Date: Fri, 17 Jun 2022 17:06:21 +0200
Subject: [PATCH] Move device-specific config to a separate module

---
 configuration.nix  | 49 ++------------------------------------------
 modules/device.nix | 51 ++++++++++++++++++++++++++++++++++++++++++++++
 2 files changed, 53 insertions(+), 47 deletions(-)
 create mode 100644 modules/device.nix

diff --git a/configuration.nix b/configuration.nix
index 329df1f..32a2004 100644
--- a/configuration.nix
+++ b/configuration.nix
@@ -3,33 +3,7 @@
 with pkgs;
 
 {
-  # NixOS wants to enable GRUB by default
-  boot.loader.grub.enable = false;
-
-  # A bunch of boot parameters needed for optimal runtime on RPi 4B
-  boot.kernelPackages = linuxPackages_rpi4;
-  boot.kernelParams = [
-    "zfs.zfs_arc_max=134217728"
-    "console=TTYAMA0,115200"
-    "console=tty1"
-    "8250.nr_uarts=1"
-    "iomem=relaxed"
-    "strict-devmem=0"
-  ];
-
-  # Enable SATA-HAT GPIO features
-  boot.loader.raspberryPi = {
-    enable = true;
-    version = 4;
-    firmwareConfig = ''
-      iomem=relaxed
-      strict-devmem=0
-      dtoverlay=pwm-2chan,pin=12,func=4,pin2=13,func2=4
-      dtoverlay=w1-gpio
-      dtparam=i2c1=on
-    '';
-  };
-
+  # Declare system packages
   environment.systemPackages = [
     libraspberrypi
     htop
@@ -38,26 +12,6 @@ with pkgs;
     inputs.agenix.defaultPackage.aarch64-linux
   ];
 
-  # Load PWM hardware timers
-  boot.kernelModules = [ "pwm_bcm2835" "w1-gpio" "w1-therm" ];
-  hardware.deviceTree = {
-    enable = true;
-    filter = "*-rpi-*.dtb";
-    overlays = [
-      {
-        name = "pwm-2chan";
-        dtboFile = "${device-tree_rpi.overlays}/pwm-2chan.dtbo";
-      }
-      {
-        name = "w1-gpio";
-        dtboFile = "${device-tree_rpi.overlays}/w1-gpio.dtbo";
-      }
-    ];
-  };
-
-  # Enable I2C
-  hardware.i2c.enable = true;
-
   # Add a swap file
   swapDevices = [{
     device = "/swapfile";
@@ -208,6 +162,7 @@ with pkgs;
     ./modules/periodic.nix
     ./modules/communication.nix
     ./modules/information.nix
+    ./modules/device.nix
   ];
 
 }
diff --git a/modules/device.nix b/modules/device.nix
new file mode 100644
index 0000000..f0cc77d
--- /dev/null
+++ b/modules/device.nix
@@ -0,0 +1,51 @@
+{ config, lib, pkgs, ... }:
+
+with pkgs;
+
+{
+  # A bunch of boot parameters needed for optimal runtime on RPi 4B
+  boot.kernelPackages = linuxPackages_rpi4;
+  boot.kernelParams = [
+    "zfs.zfs_arc_max=134217728"
+    "console=TTYAMA0,115200"
+    "console=tty1"
+    "8250.nr_uarts=1"
+    "iomem=relaxed"
+    "strict-devmem=0"
+  ];
+
+  # Enable SATA-HAT GPIO features
+  boot.loader.grub.enable = false;
+  boot.loader.raspberryPi = {
+    enable = true;
+    version = 4;
+    firmwareConfig = ''
+      iomem=relaxed
+      strict-devmem=0
+      dtoverlay=pwm-2chan,pin=12,func=4,pin2=13,func2=4
+      dtoverlay=w1-gpio
+      dtparam=i2c1=on
+    '';
+  };
+
+  # Load PWM hardware timers
+  boot.kernelModules = [ "pwm_bcm2835" "w1-gpio" "w1-therm" ];
+  hardware.deviceTree = {
+    enable = true;
+    filter = "*-rpi-*.dtb";
+    overlays = [
+      {
+        name = "pwm-2chan";
+        dtboFile = "${device-tree_rpi.overlays}/pwm-2chan.dtbo";
+      }
+      {
+        name = "w1-gpio";
+        dtboFile = "${device-tree_rpi.overlays}/w1-gpio.dtbo";
+      }
+    ];
+  };
+
+  # Enable I2C
+  hardware.i2c.enable = true;
+
+}