From 608982d8f27a0f74d73e70cdfcf04841a72cb212 Mon Sep 17 00:00:00 2001 From: Zynh Ludwig Date: Fri, 28 Jun 2024 07:48:26 -0700 Subject: [PATCH] more rework --- flake.nix | 49 ++++++++++++++++++++++++++++++------------------- lib/default.nix | 16 ++++++++++++++++ 2 files changed, 46 insertions(+), 19 deletions(-) create mode 100644 lib/default.nix diff --git a/flake.nix b/flake.nix index c4231b6..514aa48 100644 --- a/flake.nix +++ b/flake.nix @@ -17,29 +17,40 @@ }; }; - outputs = { self, nixpkgs, ... }@inputs: + outputs = + { self, nixpkgs, ... }@inputs: { nixosConfigurations = let + lib = import ./lib { inherit (nixpkgs) lib; }; + cleanHostname = config: builtins.removeAttrs config [ "hostname" ]; - toNixosConfigPart = config: { name = config.hostname; value = nixpkgs.lib.nixosSystem (cleanHostname config); }; + toPartialNixosConfigSet = config: { name = config.hostname; value = nixpkgs.lib.nixosSystem (cleanHostname config); }; + toNixosSystemConfig = { hostname, system }: { + inherit system hostname; + specialArgs = { inherit inputs; }; + modules = [ + ./hosts/${hostname}/hardware-configuration.nix + ./hosts/${hostname}/configuration.nix + inputs.home-manager.nixosModules.default + ]; + }; + compileSystems = systems: lib.right builtins.listToAttrs (map toPartialNixosConfigSet) (map toNixosSystemConfig) systems; in - builtins.listToAttrs - (map toNixosConfigPart - (map - ({ hostname, system }: { - inherit system hostname; - specialArgs = { inherit inputs; }; - modules = [ - ./hosts/${hostname}/hardware-configuration.nix - ./hosts/${hostname}/configuration.nix - inputs.home-manager.nixosModules.default - ]; - }) - [ - { hostname = "snowhawk"; system = "x86_64-linux"; } - { hostname = "sprite"; system = "aarch64-linux"; } - { hostname = "nixos"; system = "x86_64-linux"; } - ])); + compileSystems + [ + { + hostname = "snowhawk"; + system = "x86_64-linux"; + } + { + hostname = "sprite"; + system = "aarch64-linux"; + } + { + hostname = "nixos"; + system = "x86_64-linux"; + } + ]; }; } diff --git a/lib/default.nix b/lib/default.nix new file mode 100644 index 0000000..ae27ddc --- /dev/null +++ b/lib/default.nix @@ -0,0 +1,16 @@ +{ lib, ... }: +with lib; +rec { + # Ternary operator + # Exaample: + # tern false 1 2 => 2 + # tern true 1 2 => 1 + tern = pred: x: y: if pred then x else y; + + # Right-associate and chain following single-operand functions + # Example: + # right f g h 1 => f(g(h(1))) + right = f: g: tern (isFunction g) + (right (x: f (g (x)))) + (f (g)); +}