From 8349809feceac911251113100b380bf2d0a1f471 Mon Sep 17 00:00:00 2001 From: Chris Kruining Date: Sun, 21 Jul 2024 00:07:19 +0200 Subject: [PATCH] Working on modularizing stuff --- default.nix | 22 +++++++ flake.nix | 59 +++++++++++++++---- hosts/laptop/default.nix | 2 + hosts/pc/default.nix | 36 +++++++++++ .../configuration.nix => pc/default.nix.back} | 30 +--------- .../hardware-configuration.nix | 0 hosts/server/default.nix | 2 + lib/attrs.nix | 21 +++++++ lib/default.nix | 22 +++++++ lib/modules.nix | 36 +++++++++++ lib/nixos.nix | 30 ++++++++++ modules/desktop/plasma.nix | 6 +- modules/system/boot.nix | 2 + modules/system/networking.nix | 7 +++ shell.nix | 22 +++++++ 15 files changed, 256 insertions(+), 41 deletions(-) create mode 100644 default.nix create mode 100644 hosts/laptop/default.nix create mode 100644 hosts/pc/default.nix rename hosts/{default/configuration.nix => pc/default.nix.back} (76%) rename hosts/{default => pc}/hardware-configuration.nix (100%) create mode 100644 hosts/server/default.nix create mode 100644 lib/attrs.nix create mode 100644 lib/default.nix create mode 100644 lib/modules.nix create mode 100644 lib/nixos.nix create mode 100644 modules/system/networking.nix create mode 100644 shell.nix diff --git a/default.nix b/default.nix new file mode 100644 index 0000000..b127f53 --- /dev/null +++ b/default.nix @@ -0,0 +1,22 @@ +{ inputs, config, lib, pkgs, ... }: +let + inherit (builtins) toString; + inherit (lib.attrsets) attrValues filterAttrs mapAttrs mapAttrsToList; + inherit (lib.modules) mkAliasOptionModule mkDefault mkIf; + inherit (lib.my) mapModulesRec'; +in +{ + imports = [ + inputs.home-manager.nixosModules.home-manager + (mkAliasOptionModule ["hm"] ["home-manager" "users" config.user.name]) + ] + ++ (mapModulesRec' (toString ./modules) import); + + environments.variables = { + }; + + system = { + stateVersion = "23.11"; + configurationRevision = with inputs; mkIf (self ? rev) self.rev; + }; +} diff --git a/flake.nix b/flake.nix index df3a4b2..d746005 100644 --- a/flake.nix +++ b/flake.nix @@ -12,17 +12,54 @@ stylix.url = "github:danth/stylix"; }; - outputs = { self, nixpkgs, ... }@inputs: { - nixosConfigurations = { - default = nixpkgs.lib.nixosSystem { - specialArgs = {inherit inputs;}; - modules = [ - ./hosts/default/configuration.nix - inputs.home-manager.nixosModules.default - inputs.stylix.nixosModules.stylix - ]; - }; + outputs = { self, nixpkgs, ... }@inputs: let + inherit (lib.my) mapModules mapModules mapHosts; - }; + mkPkgs = pkgs: extraOverlays: + import pkgs { + inherit system; + config.allowUnfree = true; + overlays = extraOverlays ++ (lib.attrValues self.overlays); + }; + pkgs = mkPkgs nixpkgs [self.overlays.default]; + pkgs-unstable = mkPkgs nixpkgs-unstable []; + + lib = nixpkgs.lib.extend (final: prev: { + my = import ./lib { + inherit pkgs inputs; + + lib = final; + }; + }); + in { + lib = lib.my; + + packages."${system}" = mapModules ./paclages (p: pkgs.callPackage p {}); + + nixosModules = + { + chris = import ./.; + } + // mapModulesRec ./modules import; + + nixosConfigurations = mapHosts ./hosts {}; + + devShells."${system}".default = import ./shell.nix { inherit lib pkgs; }; + + + +# nixosConfigurations = { +# pc = nixpkgs.lib.nixosSystem { +# specialArgs = {inherit inputs;}; +# modules = [ +# ./hosts/pc/default.nix +# inputs.home-manager.nixosModules.default +# inputs.stylix.nixosModules.stylix +# ]; +# }; +# }; + + + }; } diff --git a/hosts/laptop/default.nix b/hosts/laptop/default.nix new file mode 100644 index 0000000..6416a04 --- /dev/null +++ b/hosts/laptop/default.nix @@ -0,0 +1,2 @@ +{}: +{} diff --git a/hosts/pc/default.nix b/hosts/pc/default.nix new file mode 100644 index 0000000..7f81450 --- /dev/null +++ b/hosts/pc/default.nix @@ -0,0 +1,36 @@ +{ config, lib, pkgs, ... }: +{ + imports = [ ./hardware-configuration.nix ]; + + modules = { + themes.active = "everforrest"; + + desktop = { + plasma.enable = true; + + terminal = { + default = "alacritty"; + allacrity.enable = true; + }; + + editors = { + default = "nano"; + nano.enable = true; + }; + + browsers = { + default = "firefox"; + firefox.enable = true; + }; + }; + + shell = { + default = "zsh"; + toolset = { + git.enable = true; + gnupg.enable = true; + }; + }; + }; +} + diff --git a/hosts/default/configuration.nix b/hosts/pc/default.nix.back similarity index 76% rename from hosts/default/configuration.nix rename to hosts/pc/default.nix.back index 08b07c4..2b3a830 100644 --- a/hosts/default/configuration.nix +++ b/hosts/pc/default.nix.back @@ -3,6 +3,7 @@ imports = [ ./hardware-configuration.nix ../../modules/system/boot.nix + ../../modules/system/networking.nix ../../modules/system/audio.nix ../../modules/system/zsa_voyager.nix @@ -25,33 +26,8 @@ allowUnfree = true; }; - networking.hostName = "chris-pc"; - # Pick only one of the below networking options. - # networking.wireless.enable = true; # Enables wireless support via wpa_supplicant. - networking.networkmanager.enable = true; # Easiest to use and most distros use this by default. - nix.settings.experimental-features = [ "nix-command" "flakes" ]; - # Set your time zone. - time.timeZone = "Europe/Amsterdam"; - - # Enable the X11 windowing system. - services.xserver.enable = true; - - - # Enable sound. - sound.enable = false; - hardware.pulseaudio.enable = false; - users.extraGroups.audio.members = [ "chris" ]; - security.rtkit.enable = true; - services.pipewire = { - enable = true; - alsa.enable = true - alsa.support32Bit = true; - pulse.enable = true; - jack.enable = true; - }; - # Define a user account. Don't forget to set a password with ‘passwd’. users.users.chris = { isNormalUser = true; @@ -76,7 +52,6 @@ # chromium thunderbird zoxide - bottles atuin btop dust @@ -86,9 +61,6 @@ nextcloud-client ]; - # session variable for chrome/electron wayland - environment.sessionVariables.NIXOS_OZONE_WL = "1"; - systemd.services.numLockOnTty = { wantedBy = [ "multi-user.target" ]; serviceConfig = { diff --git a/hosts/default/hardware-configuration.nix b/hosts/pc/hardware-configuration.nix similarity index 100% rename from hosts/default/hardware-configuration.nix rename to hosts/pc/hardware-configuration.nix diff --git a/hosts/server/default.nix b/hosts/server/default.nix new file mode 100644 index 0000000..6416a04 --- /dev/null +++ b/hosts/server/default.nix @@ -0,0 +1,2 @@ +{}: +{} diff --git a/lib/attrs.nix b/lib/attrs.nix new file mode 100644 index 0000000..9a576b2 --- /dev/null +++ b/lib/attrs.nix @@ -0,0 +1,21 @@ +{ lib, ... }: +let + inherit (lib.lists) any count; + inherit (lib.attrsets) filterAttrs listToAttrs mapAttrs' mapAttrsToList; +in rec +{ + attrsToList = attrs: + mapAttrsToList (name: value: { inherit name value; }) attrs; + + mapFilterAttrs = pred: f: attrs: + filterAttrs pred (mapAttrs' f attrs); + + getAttrs' = values: f: + listToAttrs (map f values); + + anyAttrs = pred: attrs: + any (attr: pred attr.name attr.value) (attrsToList attrs); + + countAttrs = pred: attrs: + count (attr: pred attr.name attr.value) (attrsToList attrs); +} diff --git a/lib/default.nix b/lib/default.nix new file mode 100644 index 0000000..249c76c --- /dev/null +++ b/lib/default.nix @@ -0,0 +1,22 @@ +{ inputs, lib, pkgs, ... }: +let + inherit (lib.attrsets) attrValues; + inherit (lib.fixedPoints) makeExtensible; + inherit (lib.lists) foldr; + inherit (modules) mapModules; + + modules = import ./modules.nix { + inherit lib; + + self.attrs = import ./attrs.nix { + inherit lib; + + self = {}; + }; + }; + + mylib = makeExensible (self: + mapModules ./. (file: import file { inherit self lib pkgs inputs; }) + ); +in + mylib.extend (self: super: foldr (a: b: a // b) {} (attrValues super)) diff --git a/lib/modules.nix b/lib/modules.nix new file mode 100644 index 0000000..06ed5e4 --- /dev/null +++ b/lib/modules.nix @@ -0,0 +1,36 @@ +{ lib, self, ... }: +let + inherit (builtins) attrValues readDir pathExists concatLists; + inherit (lib.attrsets) mapAttrsToList filterAttrs nameValuePair; + inherit (lib.strings) hasPrefix hasSuffix removeSuffix; + inherit (lib.trivial) id; + inherit (self.attrs) mapFilterAttrs; +in rec +{ + mapModules = dir: fn: + mapFilterAttrs (n: v: v != null && !(hasPrefix "_" n)) (n: v: let path = "${toString dir}/${n}"; in + if v == "directory" && pathExists "${path}/default.nix" + then nameValuePair n (fn path) + else if v == "regular" && n != "default.nix" && hasSuffix ".nix" n + then nameValuePair (removeSuffix ".nix" n) (fn path) + else nameValuePair "" null + ) (readDir dir); + + mapModules' = dir: fn: attrValues (mapModules dir fn); + + mapModulesRec = dir: fn: + mapFilterAttrs (n: v: v != null && !(hasPrefix "_" n)) (n: v: let path = "${toString dir}/${n}"; in + if v == "directory" && pathExists "${path}/default.nix" + then nameValuePair n (mapModulesRec path fn) + else if v == "regular" && n != "default.nix" && hasSuffix ".nix" n + then nameValuePair (removeSuffix ".nix" n) (fn path) + else nameValuePair "" null + ) (readDir dir); + + mapModulesRec' = dir: fn: let + dirs = mapAttrsToList (k: _: "${dir}/${k}") (filterAttrs (n: v: v == "directory" && !(hasPrefix "_" n)) (readDir dir)); + files = attrValues (mapModules dir id); + paths = files ++ contactLists (map (d: mapModulesRec' d id) dirs); + in + map fn paths; +} diff --git a/lib/nixos.nix b/lib/nixos.nix new file mode 100644 index 0000000..94312eb --- /dev/null +++ b/lib/nixos.nix @@ -0,0 +1,30 @@ +{ inputs, lib, pkgs, self, ... }: +let + inherit (inputs.nixpkgs.lib) nixosSystem; + inherit (builtins) baseNameOf elem; + inherit (lib.attrsets) filterAttrs; + inherit (lib.modules) mkDefault; + inherit (lib.strings) removeSuffix; + inherit (self.modules) mapModules; +in rec +{ + mkHost = path: attrs @ {sytem ? "x86_64-linux", ...}: + nixosSystem { + inherit system; + + specialArgs = { inherit lib input system; }; + + modules = [ + { + nixpkgs.pkgs = pkgs; + networking.hostName = mkDefault (removeSuffix ".nix" (baseNameOf path)); + } + (filterAttrs (n: v: !elem n ["system"]) attrs) + ../. # ../default.nix + (import path) + ]; + }; + + mapHosts = dir: attrs @ { system ? system, ... }: + mapModules dir (hostPath: mkHost hostPath attrs); +} diff --git a/modules/desktop/plasma.nix b/modules/desktop/plasma.nix index 04029b6..7f59e9b 100644 --- a/modules/desktop/plasma.nix +++ b/modules/desktop/plasma.nix @@ -1,6 +1,8 @@ { pkgs, ... }: { services = { + xserver.enable = true; + displayManager = { sddm = { enable = true; @@ -11,9 +13,11 @@ user = "chris"; }; }; + + desktopManager.plasma6.enable = true; }; - services.desktopManager.plasma6.enable = true; + environment.sessionVariables.NIXOS_OZONE_WL = "1"; environment.plasma6.excludePackages = with pkgs.kdePackages; [ konsole diff --git a/modules/system/boot.nix b/modules/system/boot.nix index b54ae1e..edb3223 100644 --- a/modules/system/boot.nix +++ b/modules/system/boot.nix @@ -1,6 +1,8 @@ {}: { boot.loader.systemd-boot-enable = true; + + time.timeZone = "Europe/Amsterdam"; fileSystems."/home/chris/new_games" = { device = "/dev/disk/by-label/games"; diff --git a/modules/system/networking.nix b/modules/system/networking.nix new file mode 100644 index 0000000..605476a --- /dev/null +++ b/modules/system/networking.nix @@ -0,0 +1,7 @@ +{ pkgs, ... }: +{ + networking = { + hostName = "chris-pc"; + networkmanager.enable = true; + }; +} diff --git a/shell.nix b/shell.nix new file mode 100644 index 0000000..22a29d0 --- /dev/null +++ b/shell.nix @@ -0,0 +1,22 @@ +{ lib, pkgs ? import {} }: +let + inherit (lib.attrsets) attrValues; + inherit (lib.meta) getExe; +in + pkgs.mkShell { + buildInputs = attrValues { + inherit (pkgs) git nix-bash-completions; + }; + + shellHook = let + inherit (pkgs) nixStable writeShelScriptBin; + + nixBin = writeShellScriptBin "nix" '' + ${ getExe nixStable} --option experimental-features "nix-command flakes" "$@" + ''; + in '' + export FLAKE="$(pwd)" + export PATH="$FLAKE/bin:${nixBin}/bin:$PATH" + ''; + } +