feat: my custom libAttrs

This commit is contained in:
Ryan Yin
2023-12-23 10:56:33 +08:00
parent 27dd5ebb13
commit 82cfa4c6f7
6 changed files with 65 additions and 22 deletions

View File

@@ -21,7 +21,7 @@
}: let }: let
constants = import ./constants.nix; 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); forEachSystem = func: (nixpkgs.lib.genAttrs constants.allSystems func);
allSystemConfigurations = import ./systems {inherit self inputs constants;}; allSystemConfigurations = import ./systems {inherit self inputs constants;};

46
lib/attrs.nix Normal file
View File

@@ -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;
}

View File

@@ -1,7 +1,6 @@
args: args:
with args; with args;
with allSystemAttrs; let with allSystemAttrs; let
inherit (nixpkgs) lib;
colmenaSystem = import ../lib/colmenaSystem.nix; colmenaSystem = import ../lib/colmenaSystem.nix;
# x86_64 related # x86_64 related
x64_base_args = { x64_base_args = {
@@ -66,36 +65,36 @@ in {
}; };
# proxmox virtual machines(x86_64) # proxmox virtual machines(x86_64)
aquamarine = colmenaSystem (lib.attrsets.mergeAttrsList [ aquamarine = colmenaSystem (libAttrs.mergeAttrsList [
x64_base_args x64_base_args
idol_aquamarine_modules idol_aquamarine_modules
{host_tags = idol_aquamarine_tags;} {host_tags = idol_aquamarine_tags;}
]); ]);
ruby = colmenaSystem (lib.attrsets.mergeAttrsList [ ruby = colmenaSystem (libAttrs.mergeAttrsList [
x64_base_args x64_base_args
idol_ruby_modules idol_ruby_modules
{host_tags = idol_ruby_tags;} {host_tags = idol_ruby_tags;}
]); ]);
kana = colmenaSystem (lib.attrsets.mergeAttrsList [ kana = colmenaSystem (libAttrs.mergeAttrsList [
x64_base_args x64_base_args
idol_kana_modules idol_kana_modules
{host_tags = idol_kana_tags;} {host_tags = idol_kana_tags;}
]); ]);
# riscv64 SBCs # riscv64 SBCs
nozomi = colmenaSystem (lib.attrsets.mergeAttrsList [ nozomi = colmenaSystem (libAttrs.mergeAttrsList [
lpi4a_base_args lpi4a_base_args
rolling_nozomi_modules rolling_nozomi_modules
{host_tags = rolling_nozomi_tags;} {host_tags = rolling_nozomi_tags;}
]); ]);
yukina = colmenaSystem (lib.attrsets.mergeAttrsList [ yukina = colmenaSystem (libAttrs.mergeAttrsList [
lpi4a_base_args lpi4a_base_args
rolling_yukina_modules rolling_yukina_modules
{host_tags = rolling_yukina_tags;} {host_tags = rolling_yukina_tags;}
]); ]);
# aarch64 SBCs # aarch64 SBCs
suzu = colmenaSystem (lib.attrsets.mergeAttrsList [ suzu = colmenaSystem (libAttrs.mergeAttrsList [
rk3588_base_args rk3588_base_args
_12kingdoms_suzu_modules _12kingdoms_suzu_modules
{host_tags = _12kingdoms_suzu_tags;} {host_tags = _12kingdoms_suzu_tags;}

View File

@@ -1,7 +1,6 @@
args: args:
with args; with args;
with allSystemAttrs; let with allSystemAttrs; let
inherit (nixpkgs) lib;
macosSystem = import ../lib/macosSystem.nix; macosSystem = import ../lib/macosSystem.nix;
base_args = { base_args = {
inherit nix-darwin home-manager; inherit nix-darwin home-manager;
@@ -11,7 +10,7 @@ in {
# macOS's configuration # macOS's configuration
darwinConfigurations = { darwinConfigurations = {
harmonica = macosSystem ( harmonica = macosSystem (
lib.attrsets.mergeAttrsList [ libAttrs.mergeAttrsList [
base_args base_args
darwin_harmonica_modules darwin_harmonica_modules
{ {
@@ -22,7 +21,7 @@ in {
); );
fern = macosSystem ( fern = macosSystem (
lib.attrsets.mergeAttrsList [ libAttrs.mergeAttrsList [
base_args base_args
darwin_fern_modules darwin_fern_modules
{ {

View File

@@ -4,11 +4,13 @@
constants, constants,
}: let }: let
inherit (inputs.nixpkgs) lib; inherit (inputs.nixpkgs) lib;
libAttrs = import ../lib/attrs.nix {inherit lib;};
vars = import ./vars.nix; vars = import ./vars.nix;
specialArgsForSystem = system: specialArgsForSystem = system:
{ {
inherit (constants) username userfullname useremail; inherit (constants) username userfullname useremail;
inherit libAttrs;
# use unstable branch for some packages to get the latest updates # use unstable branch for some packages to get the latest updates
pkgs-unstable = import inputs.nixpkgs-unstable { pkgs-unstable = import inputs.nixpkgs-unstable {
inherit system; # refer the `system` parameter form outer scope recursively inherit system; # refer the `system` parameter form outer scope recursively
@@ -19,18 +21,18 @@
// inputs; // inputs;
allSystemSpecialArgs = allSystemSpecialArgs =
lib.attrsets.mapAttrs libAttrs.mapAttrs
(name: specialArgsForSystem) (_: specialArgsForSystem)
constants.allSystemAttrs; constants.allSystemAttrs;
args = lib.attrsets.mergeAttrsList [ args = libAttrs.mergeAttrsList [
inputs inputs
constants constants
vars vars
{inherit self allSystemSpecialArgs;} {inherit self lib libAttrs allSystemSpecialArgs;}
]; ];
in in
lib.attrsets.mergeAttrsList [ libAttrs.mergeAttrsList [
(import ./nixos.nix args) (import ./nixos.nix args)
(import ./darwin.nix args) (import ./darwin.nix args)
(import ./colmena.nix args) (import ./colmena.nix args)

View File

@@ -1,7 +1,6 @@
args: args:
with args; with args;
with allSystemAttrs; let with allSystemAttrs; let
inherit (nixpkgs) lib;
nixosSystem = import ../lib/nixosSystem.nix; nixosSystem = import ../lib/nixosSystem.nix;
base_args = { base_args = {
@@ -25,11 +24,9 @@ in {
# take system images for idols # take system images for idols
# https://github.com/nix-community/nixos-generators # 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) libAttrs.listToAttrs
# => { foo = "x_foo"; bar = "x_bar"; }
nixpkgs.lib.genAttrs
[ [
"ai_i3" "ai_i3"
"ai_hyprland" "ai_hyprland"
@@ -39,7 +36,7 @@ in {
) )
( (
nixpkgs.lib.genAttrs libAttrs.listToAttrs
[ [
"aquamarine" "aquamarine"
"ruby" "ruby"