From ba6b30ca950c679fe7141ef35c7f72de9bd5bb7d Mon Sep 17 00:00:00 2001 From: Gabriel Arazas Date: Sun, 3 Mar 2024 16:39:32 +0800 Subject: [PATCH] lib: standardize arguments (again) We'll just copy what we did with Bahaghari project. --- lib/default.nix | 45 ++++--------------- lib/home-manager.nix | 4 +- lib/nixos.nix | 8 ++-- lib/nixvim.nix | 2 +- lib/sops.nix | 8 ++-- lib/trivial.nix | 39 ++++++++++++++++ .../home-manager/_private/extra-arguments.nix | 4 +- modules/nixos/_private/extra-arguments.nix | 4 +- modules/nixvim/_private/extra-arguments.nix | 2 +- 9 files changed, 63 insertions(+), 53 deletions(-) create mode 100644 lib/trivial.nix diff --git a/lib/default.nix b/lib/default.nix index 7192ac25..2c42167b 100644 --- a/lib/default.nix +++ b/lib/default.nix @@ -2,41 +2,12 @@ { pkgs }: pkgs.lib.makeExtensible - (self: - rec { - /* Count the attributes with the given predicate. +(self: + let + inherit (pkgs) lib; + callLib = file: import file { inherit pkgs lib self; }; + in { + trivial = callLib ./trivial.nix; - Examples: - countAttrs (name: value: value) { d = true; f = true; a = false; } - => 2 - - countAttrs (name: value: value.enable) { d = { enable = true; }; f = { enable = false; package = [ ]; }; } - => 1 - */ - countAttrs = pred: attrs: - pkgs.lib.count (attr: pred attr.name attr.value) - (pkgs.lib.mapAttrsToList pkgs.lib.nameValuePair attrs); - - /* Returns the file path of the given config of the given environment. - - Type: getConfig :: String -> String -> Path - - Example: - getConfig "home-manager" "foo-dogsquared" - => ../configs/home-manager/foo-dogsquared - */ - getConfig = type: config: ../configs/${type}/${config}; - - - /* Returns the file path of the given user subpart of the given - environment. Only certain environments such as NixOS have this type of - setup. - - Type: getConfig :: String -> String -> Path - - Example: - getUser "nixos" "foo-dogsquared" - => ../configs/nixos/_users/foo-dogsquared - */ - getUser = type: user: ../configs/${type}/_users/${user}; - }) + inherit (self.trivial) countAttrs getConfig getUser; + }) diff --git a/lib/home-manager.nix b/lib/home-manager.nix index b4fd04b6..74df1467 100644 --- a/lib/home-manager.nix +++ b/lib/home-manager.nix @@ -1,5 +1,5 @@ # Custom libraries for home-manager library. -{ pkgs, lib }: +{ pkgs, lib, self }: rec { /* @@ -15,7 +15,7 @@ rec { # The default value when `attrPath` is missing. default: - attrs ? nixosConfig && pkgs.lib.attrByPath attrPath default attrs; + attrs ? nixosConfig && lib.attrByPath attrPath default attrs; hasDarwinConfigAttr = # The configuration attribute set of the home-manager configuration. diff --git a/lib/nixos.nix b/lib/nixos.nix index 8c479c87..2320024d 100644 --- a/lib/nixos.nix +++ b/lib/nixos.nix @@ -1,14 +1,14 @@ # All of the functions suitable only for NixOS. -{ pkgs, lib }: +{ pkgs, lib, self }: -rec { +{ # Checks if the NixOS configuration is part of the nixos-generator build. # Typically, we just check if there's a certain attribute that is imported # from it. hasNixosFormat = config: - pkgs.lib.hasAttrByPath [ "formatAttr" ] config; + lib.hasAttrByPath [ "formatAttr" ] config; # Checks if the NixOS config is being built for a particular format. isFormat = config: format: - hasNixosFormat config && config.formatAttr == format; + (config.formatAttr or "") == format; } diff --git a/lib/nixvim.nix b/lib/nixvim.nix index 57afe603..3a2a8379 100644 --- a/lib/nixvim.nix +++ b/lib/nixvim.nix @@ -1,4 +1,4 @@ -{ pkgs, lib }: +{ pkgs, lib, self }: { isStandalone = config: diff --git a/lib/sops.nix b/lib/sops.nix index e16a409e..fc384e23 100644 --- a/lib/sops.nix +++ b/lib/sops.nix @@ -1,5 +1,5 @@ # A library specifically for environments with sops-nix. -{ pkgs, lib }: +{ pkgs, lib, self }: { /* Get the secrets from a given sops file. This will set the individual @@ -21,7 +21,7 @@ let getKey = key: { inherit key sopsFile; }; in - pkgs.lib.mapAttrs + lib.mapAttrs (path: attrs: (getKey path) // attrs) secrets; @@ -44,9 +44,9 @@ })) */ attachSopsPathPrefix = prefix: secrets: - pkgs.lib.mapAttrs' + lib.mapAttrs' (key: settings: - pkgs.lib.nameValuePair + lib.nameValuePair "${prefix}/${key}" ({ inherit key; } // settings)) secrets; diff --git a/lib/trivial.nix b/lib/trivial.nix new file mode 100644 index 00000000..ca39f8bc --- /dev/null +++ b/lib/trivial.nix @@ -0,0 +1,39 @@ +{ pkgs, lib, self }: + +{ + /* Count the attributes with the given predicate. + + Examples: + countAttrs (name: value: value) { d = true; f = true; a = false; } + => 2 + + countAttrs (name: value: value.enable) { d = { enable = true; }; f = { enable = false; package = [ ]; }; } + => 1 + */ + countAttrs = pred: attrs: + lib.count (attr: pred attr.name attr.value) + (lib.mapAttrsToList lib.nameValuePair attrs); + + /* Returns the file path of the given config of the given environment. + + Type: getConfig :: String -> String -> Path + + Example: + getConfig "home-manager" "foo-dogsquared" + => ../configs/home-manager/foo-dogsquared + */ + getConfig = type: config: ../configs/${type}/${config}; + + + /* Returns the file path of the given user subpart of the given + environment. Only certain environments such as NixOS have this type of + setup. + + Type: getConfig :: String -> String -> Path + + Example: + getUser "nixos" "foo-dogsquared" + => ../configs/nixos/_users/foo-dogsquared + */ + getUser = type: user: ../configs/${type}/_users/${user}; +} diff --git a/modules/home-manager/_private/extra-arguments.nix b/modules/home-manager/_private/extra-arguments.nix index 5c279cb8..2085d87d 100644 --- a/modules/home-manager/_private/extra-arguments.nix +++ b/modules/home-manager/_private/extra-arguments.nix @@ -8,8 +8,8 @@ in { _module.args.foodogsquaredLib = foodogsquaredLib.extend (final: prev: { - home-manager = import ../../../lib/home-manager.nix { inherit pkgs; lib = prev; }; + home-manager = import ../../../lib/home-manager.nix { inherit pkgs lib; self = final; }; } // lib.optionalAttrs (options?sops) { - sops-nix = import ../../../lib/sops.nix { inherit pkgs; lib = prev; }; + sops-nix = import ../../../lib/sops.nix { inherit pkgs lib; self = final; }; }); } diff --git a/modules/nixos/_private/extra-arguments.nix b/modules/nixos/_private/extra-arguments.nix index c1602f02..41ed0ebe 100644 --- a/modules/nixos/_private/extra-arguments.nix +++ b/modules/nixos/_private/extra-arguments.nix @@ -8,8 +8,8 @@ in { _module.args.foodogsquaredLib = foodogsquaredLib.extend (final: prev: { - nixos = import ../../../lib/nixos.nix { inherit pkgs; lib = final; }; + nixos = import ../../../lib/nixos.nix { inherit pkgs lib; self = final; }; } // lib.optionalAttrs (options?sops) { - sops-nix = import ../../../lib/sops.nix { inherit pkgs; lib = final; }; + sops-nix = import ../../../lib/sops.nix { inherit pkgs lib; self = final; }; }); } diff --git a/modules/nixvim/_private/extra-arguments.nix b/modules/nixvim/_private/extra-arguments.nix index 934bdb21..bfc64aa3 100644 --- a/modules/nixvim/_private/extra-arguments.nix +++ b/modules/nixvim/_private/extra-arguments.nix @@ -7,6 +7,6 @@ in { _module.args.foodogsquaredLib = foodogsquaredLib.extend (final: prev: { - nixvim = import ../../../lib/nixvim.nix { inherit pkgs; lib = prev; }; + nixvim = import ../../../lib/nixvim.nix { inherit pkgs lib; self = final; }; }); }