From 331b7cf8e33620d9bd3f43e8c4666825a73af058 Mon Sep 17 00:00:00 2001 From: Bruno BELANYI Date: Wed, 10 Mar 2021 18:49:12 +0000 Subject: [PATCH] lib: create 'my' extensions Inspired by this [1] repo, I want to add some functions to `lib.my`. I would have liked to make it work without having to use an overlay, but I did not manage to do it... [1]: https://github.com/hlissner/dotfiles --- flake.nix | 15 ++++++++++++--- lib/attrs.nix | 7 +++++++ lib/default.nix | 21 +++++++++++++++++++++ lib/modules.nix | 23 +++++++++++++++++++++++ 4 files changed, 63 insertions(+), 3 deletions(-) create mode 100644 lib/attrs.nix create mode 100644 lib/default.nix create mode 100644 lib/modules.nix diff --git a/flake.nix b/flake.nix index 28f9f20..760e720 100644 --- a/flake.nix +++ b/flake.nix @@ -8,11 +8,14 @@ futils.url = "github:numtide/flake-utils"; }; - outputs = { self, nixpkgs, nur, home-manager, futils }: + outputs = { self, nixpkgs, nur, home-manager, futils } @ inputs: let - inherit (nixpkgs) lib; inherit (futils.lib) eachDefaultSystem; + lib = nixpkgs.lib.extend (self: super: { + my = import ./lib { inherit inputs; pkgs = nixpkgs; lib = self; }; + }); + defaultModules = [ ({ ... }: { # Let 'nixos-version --json' know about the Git revision @@ -21,7 +24,7 @@ then self.rev else throw "Refusing to build from a dirty Git tree!"; }) - { nixpkgs.overlays = [ nur.overlay ]; } + { nixpkgs.overlays = [ nur.overlay self.overlay ]; } home-manager.nixosModules.home-manager { home-manager.users.ambroisie = import ./home; @@ -52,6 +55,12 @@ ]; }; }) // { + overlay = self.overlays.lib; + + overlays = { + lib = final: prev: { inherit lib; }; + }; + nixosConfigurations = lib.mapAttrs buildHost { porthos = "x86_64-linux"; }; diff --git a/lib/attrs.nix b/lib/attrs.nix new file mode 100644 index 0000000..4595467 --- /dev/null +++ b/lib/attrs.nix @@ -0,0 +1,7 @@ +{ lib, ... }: +let + inherit (lib) filterAttrs mapAttrs'; +in +{ + mapFilterAttrs = pred: f: attrs: filterAttrs pred (mapAttrs' f attrs); +} diff --git a/lib/default.nix b/lib/default.nix new file mode 100644 index 0000000..9014f85 --- /dev/null +++ b/lib/default.nix @@ -0,0 +1,21 @@ +# Extension mechanism shamelessly stolen from [1]. +# +# [1]: https://github.com/hlissner/dotfiles/blob/master/lib/default.nix +{ lib, pkgs, inputs }: +let + inherit (lib) makeExtensible attrValues foldr; + inherit (modules) mapModules; + + modules = import ./modules.nix { + inherit lib; + self.attrs = import ./attrs.nix { inherit lib; self = { }; }; + }; + + mylib = makeExtensible (self: + with 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..92e8476 --- /dev/null +++ b/lib/modules.nix @@ -0,0 +1,23 @@ +{ self, lib, ... }: +let + inherit (builtins) readDir pathExists; + inherit (lib) hasPrefix hasSuffix nameValuePair removeSuffix; + inherit (self.attrs) mapFilterAttrs; +in +{ + 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); +}