diff --git a/flake.nix b/flake.nix index 2d2991da..63993d43 100644 --- a/flake.nix +++ b/flake.nix @@ -21,7 +21,7 @@ }: let constants = import ./constants.nix; - # FYI: `lib.genAttrs [ "foo" "bar" ] (name: "x_" + name)` => `{ foo = "x_foo"; bar = "x_bar"; }` + # `lib.genAttrs [ "foo" "bar" ] (name: "x_" + name)` => `{ foo = "x_foo"; bar = "x_bar"; }` forEachSystem = func: (nixpkgs.lib.genAttrs constants.allSystems func); allSystemConfigurations = import ./systems {inherit self inputs constants;}; diff --git a/lib/attrs.nix b/lib/attrs.nix new file mode 100644 index 00000000..f1c067d9 --- /dev/null +++ b/lib/attrs.nix @@ -0,0 +1,46 @@ +{ lib, ... }: + +rec { + # Generate an attribute set from a list. + # + # lib.genAttrs [ "foo" "bar" ] (name: "x_" + name) + # => { foo = "x_foo"; bar = "x_bar"; } + listToAttrs = lib.genAttrs; + + # Update only the values of the given attribute set. + # + # mapAttrs + # (name: value: ("bar-" + value)) + # { x = "a"; y = "b"; } + # => { foo = "bar-a"; foo = "bar-b"; } + mapAttrs = lib.attrsets.mapAttrs; + + # Update both the names and values of the given attribute set. + # + # mapAttrs' + # (name: value: nameValuePair ("foo_" + name) ("bar-" + value)) + # { x = "a"; y = "b"; } + # => { foo_x = "bar-a"; foo_y = "bar-b"; } + mapAttrs' = lib.attrsets.mapAttrs'; + + # Merge a list of attribute sets into one. smilar to the operator `a // b`, but for a list of attribute sets. + # + # mergeAttrsList + # [ { x = "a"; y = "b"; } { x = "c"; z = "d"; } { g = "e"; } ] + # => { x = "c"; y = "b"; z = "d"; g = "e"; } + mergeAttrsList = lib.attrsets.mergeAttrsList; + + # Generate a string from an attribute set. + # + # attrsets.foldlAttrs + # (acc: name: value: acc + "\nexport ${name}=${value}") + # "# A shell script" + # { x = "a"; y = "b"; } + # => + # ``` + # # A shell script + # export x=a + # export y=b + # ```` + foldlAttrs = lib.attrsets.foldlAttrs; +} diff --git a/systems/colmena.nix b/systems/colmena.nix index 242127ed..94302c13 100644 --- a/systems/colmena.nix +++ b/systems/colmena.nix @@ -1,7 +1,6 @@ args: with args; with allSystemAttrs; let - inherit (nixpkgs) lib; colmenaSystem = import ../lib/colmenaSystem.nix; # x86_64 related x64_base_args = { @@ -66,36 +65,36 @@ in { }; # proxmox virtual machines(x86_64) - aquamarine = colmenaSystem (lib.attrsets.mergeAttrsList [ + aquamarine = colmenaSystem (libAttrs.mergeAttrsList [ x64_base_args idol_aquamarine_modules {host_tags = idol_aquamarine_tags;} ]); - ruby = colmenaSystem (lib.attrsets.mergeAttrsList [ + ruby = colmenaSystem (libAttrs.mergeAttrsList [ x64_base_args idol_ruby_modules {host_tags = idol_ruby_tags;} ]); - kana = colmenaSystem (lib.attrsets.mergeAttrsList [ + kana = colmenaSystem (libAttrs.mergeAttrsList [ x64_base_args idol_kana_modules {host_tags = idol_kana_tags;} ]); # riscv64 SBCs - nozomi = colmenaSystem (lib.attrsets.mergeAttrsList [ + nozomi = colmenaSystem (libAttrs.mergeAttrsList [ lpi4a_base_args rolling_nozomi_modules {host_tags = rolling_nozomi_tags;} ]); - yukina = colmenaSystem (lib.attrsets.mergeAttrsList [ + yukina = colmenaSystem (libAttrs.mergeAttrsList [ lpi4a_base_args rolling_yukina_modules {host_tags = rolling_yukina_tags;} ]); # aarch64 SBCs - suzu = colmenaSystem (lib.attrsets.mergeAttrsList [ + suzu = colmenaSystem (libAttrs.mergeAttrsList [ rk3588_base_args _12kingdoms_suzu_modules {host_tags = _12kingdoms_suzu_tags;} diff --git a/systems/darwin.nix b/systems/darwin.nix index f497810c..a26616f7 100644 --- a/systems/darwin.nix +++ b/systems/darwin.nix @@ -1,7 +1,6 @@ args: with args; with allSystemAttrs; let - inherit (nixpkgs) lib; macosSystem = import ../lib/macosSystem.nix; base_args = { inherit nix-darwin home-manager; @@ -11,7 +10,7 @@ in { # macOS's configuration darwinConfigurations = { harmonica = macosSystem ( - lib.attrsets.mergeAttrsList [ + libAttrs.mergeAttrsList [ base_args darwin_harmonica_modules { @@ -22,7 +21,7 @@ in { ); fern = macosSystem ( - lib.attrsets.mergeAttrsList [ + libAttrs.mergeAttrsList [ base_args darwin_fern_modules { diff --git a/systems/default.nix b/systems/default.nix index 2c6967a1..43dfce22 100644 --- a/systems/default.nix +++ b/systems/default.nix @@ -4,11 +4,13 @@ constants, }: let inherit (inputs.nixpkgs) lib; + libAttrs = import ../lib/attrs.nix {inherit lib;}; vars = import ./vars.nix; specialArgsForSystem = system: { inherit (constants) username userfullname useremail; + inherit libAttrs; # use unstable branch for some packages to get the latest updates pkgs-unstable = import inputs.nixpkgs-unstable { inherit system; # refer the `system` parameter form outer scope recursively @@ -19,18 +21,18 @@ // inputs; allSystemSpecialArgs = - lib.attrsets.mapAttrs - (name: specialArgsForSystem) + libAttrs.mapAttrs + (_: specialArgsForSystem) constants.allSystemAttrs; - args = lib.attrsets.mergeAttrsList [ + args = libAttrs.mergeAttrsList [ inputs constants vars - {inherit self allSystemSpecialArgs;} + {inherit self lib libAttrs allSystemSpecialArgs;} ]; in - lib.attrsets.mergeAttrsList [ + libAttrs.mergeAttrsList [ (import ./nixos.nix args) (import ./darwin.nix args) (import ./colmena.nix args) diff --git a/systems/nixos.nix b/systems/nixos.nix index 026c3beb..796dfdf0 100644 --- a/systems/nixos.nix +++ b/systems/nixos.nix @@ -1,7 +1,6 @@ args: with args; with allSystemAttrs; let - inherit (nixpkgs) lib; nixosSystem = import ../lib/nixosSystem.nix; base_args = { @@ -25,11 +24,9 @@ in { # take system images for idols # https://github.com/nix-community/nixos-generators - packages."${x64_system}" = lib.attrsets.mergeAttrsList [ + packages."${x64_system}" = libAttrs.mergeAttrsList [ ( - # lib.genAttrs [ "foo" "bar" ] (name: "x_" + name) - # => { foo = "x_foo"; bar = "x_bar"; } - nixpkgs.lib.genAttrs + libAttrs.listToAttrs [ "ai_i3" "ai_hyprland" @@ -39,7 +36,7 @@ in { ) ( - nixpkgs.lib.genAttrs + libAttrs.listToAttrs [ "aquamarine" "ruby"