From b3aa6cad817fe28dfe2caa4bcbad01c374658235 Mon Sep 17 00:00:00 2001 From: h7x4 Date: Sat, 19 Nov 2022 16:14:18 +0100 Subject: [PATCH] lib: move lib extensions into its own module argument --- flake.nix | 39 ++++++++++++---------------- home/config/ssh/hosts/pvv.nix | 6 ++--- home/home.nix | 9 +++---- home/shellOptions.nix | 35 +++++++++++++------------ hosts/common.nix | 5 ++-- {overlays/lib => lib}/attrsets.nix | 8 +++--- lib/default.nix | 8 ++++++ {overlays/lib => lib}/lists.nix | 8 +++--- {overlays/lib => lib}/strings.nix | 10 +++---- {overlays/lib => lib}/termColors.nix | 7 +++-- {overlays/lib => lib}/trivial.nix | 5 ++-- modules/machineVars.nix | 3 +-- overlays/lib/default.nix | 10 ------- 13 files changed, 70 insertions(+), 83 deletions(-) rename {overlays/lib => lib}/attrsets.nix (90%) create mode 100644 lib/default.nix rename {overlays/lib => lib}/lists.nix (62%) rename {overlays/lib => lib}/strings.nix (83%) rename {overlays/lib => lib}/termColors.nix (84%) rename {overlays/lib => lib}/trivial.nix (72%) delete mode 100644 overlays/lib/default.nix diff --git a/flake.nix b/flake.nix index 5917b75..3abd8f0 100644 --- a/flake.nix +++ b/flake.nix @@ -3,7 +3,7 @@ inputs = { nixpkgs.url = "nixpkgs/nixos-22.05"; - unstable-nixpkgs.url = "nixpkgs/nixos-unstable"; + nixpkgs-unstable.url = "nixpkgs/nixos-unstable"; home-manager = { url = "github:nix-community/home-manager/release-22.05"; @@ -46,18 +46,17 @@ outputs = inputs@{ self, nixpkgs, - unstable-nixpkgs, + nixpkgs-unstable, home-manager, vscode-server, secrets, fonts, dotfiles, - website, - ... + website }: let system = "x86_64-linux"; - pkgs = import nixpkgs { + pkgs-config = { inherit system; config = { @@ -65,26 +64,13 @@ android_sdk.accept_license = true; }; - overlays = [ self.overlays.lib ]; - }; - - unstable-pkgs = import unstable-nixpkgs { - inherit system; - - config = { - allowUnfree = true; - android_sdk.accept_license = true; - }; - - overlays = [ self.overlays.lib ]; + # overlays = [ self.overlays.lib ]; }; + pkgs = import nixpkgs pkgs-config; + unstable-pkgs = import nixpkgs-unstable pkgs-config; in { - overlays = { - lib = import ./overlays/lib; - }; - - lib = (pkgs.extend self.overlays.lib).lib; + extendedLib = import ./lib { stdlib = pkgs.lib; }; homeConfigurations = { h7x4 = home-manager.lib.homeManagerConfiguration { @@ -130,6 +116,7 @@ config._module.args = { inherit inputs; inherit unstable-pkgs; + inherit (self) extendedLib; secrets = secrets.outputs.settings; }; } @@ -138,12 +125,18 @@ { home-manager = { useGlobalPkgs = true; - extraSpecialArgs = { inherit inputs; secrets = secrets.outputs.settings; }; + extraSpecialArgs = { + inherit inputs; + inherit (self) extendedLib; + secrets = secrets.outputs.settings; + }; users.h7x4 = import ./home/home.nix { inherit pkgs; inherit inputs; + inherit (pkgs) lib; inherit (config) machineVars colors; + inherit (self) extendedLib; }; }; }) diff --git a/home/config/ssh/hosts/pvv.nix b/home/config/ssh/hosts/pvv.nix index 62ba497..d1bc15d 100644 --- a/home/config/ssh/hosts/pvv.nix +++ b/home/config/ssh/hosts/pvv.nix @@ -1,6 +1,5 @@ -{ pkgs, secrets, ... }: +{ pkgs, lib, extendedLib, secrets, ... }: let - inherit (pkgs) lib; inherit (secrets.ssh.users.pvv) normalUser adminUser; # http://www.pvv.ntnu.no/pvv/Maskiner @@ -82,10 +81,9 @@ let ]; in machines: pipe machines pipeline; - in { - programs.ssh.matchBlocks = lib.attrsets.concatAttrs [ + programs.ssh.matchBlocks = extendedLib.attrsets.concatAttrs [ (convertMachinesWith convertNormalMachine normalMachines) (convertMachinesWith convertAdminMachine rootMachines) ]; diff --git a/home/home.nix b/home/home.nix index d26f30a..0eef9fc 100644 --- a/home/home.nix +++ b/home/home.nix @@ -1,6 +1,5 @@ -{ pkgs, inputs, machineVars, colors, ... } @ args: let - inherit (pkgs) lib; - inherit (pkgs.lib) mkForce mkIf optionals; +{ pkgs, lib, extendedLib, inputs, machineVars, colors, ... } @ args: let + inherit (lib) mkForce mkIf optionals; graphics = !machineVars.headless; in { inherit machineVars; @@ -54,7 +53,7 @@ in { homeDirectory = "/home/h7x4"; file = { ".ghci".text = '' - :set prompt "${lib.termColors.front.magenta "[GHCi]λ"} ". + :set prompt "${extendedLib.termColors.front.magenta "[GHCi]λ"} ". ''; ".pyrc".text = '' @@ -63,7 +62,7 @@ in { # You also need \x01 and \x02 to separate escape sequence, due to: # https://stackoverflow.com/a/9468954/1147688 - sys.ps1='\x01\x1b${lib.termColors.front.blue "[Python]> "}\x02>>>\x01\x1b[0m\x02 ' # bright yellow + sys.ps1='\x01\x1b${extendedLib.termColors.front.blue "[Python]> "}\x02>>>\x01\x1b[0m\x02 ' # bright yellow sys.ps2='\x01\x1b[1;49;31m\x02...\x01\x1b[0m\x02 ' # bright red ''; }; diff --git a/home/shellOptions.nix b/home/shellOptions.nix index 1c97112..4e3b73e 100644 --- a/home/shellOptions.nix +++ b/home/shellOptions.nix @@ -1,7 +1,4 @@ -{ pkgs, config, ... }: let - - # FIXME: lib should be imported directly as a module argument. - inherit (pkgs) lib; +{ pkgs, lib, extendedLib, config, ... }: let sedColor = color: @@ -16,7 +13,7 @@ # Context Functors functors = let inherit (lib.strings) concatStringsSep; - inherit (lib.termColors.front) blue; + inherit (extendedLib.termColors.front) blue; genWrapper = type: value: { inherit type; inherit value; }; in { @@ -228,7 +225,7 @@ in rec { watch = "${procps}/bin/watch "; concatPdfs = shellThen [ - "echo \"${lib.termColors.front.red "Concatenating all pdfs in current directory to 'out.pdf'"}\"" + "echo \"${extendedLib.termColors.front.red "Concatenating all pdfs in current directory to 'out.pdf'"}\"" "${poppler_utils}/bin/pdfunite *.pdf out.pdf" ]; @@ -247,14 +244,14 @@ in rec { "Misc" = { youtube-dl-list = shellJoin [ - "${youtube-dl}/bin/youtube-dl" + "${yt-dlp}/bin/yt-dlp" "-f \"bestvideo[ext=mp4]+bestaudio[e=m4a]/bestvideo+bestaudio\"" "-o \"%(playlist_index)s-%(title)s.%(ext)s\"" ]; - music-dl = "${youtube-dl}/bin/youtube-dl --extract-audio -f \"bestaudio[ext=m4a]/best\""; + music-dl = "${yt-dlp}/bin/yt-dlp --extract-audio -f \"bestaudio[ext=m4a]/best\""; music-dl-list = shellJoin [ - "${youtube-dl}/bin/youtube-dl" + "${yt-dlp}/bin/yt-dlp" "--extract-audio" "-f \"bestaudio[ext=m4a]/best\"" "-o \"%(playlist_index)s-%(title)s.%(ext)s\"" @@ -273,8 +270,12 @@ in rec { "Generated" = { "cds" = let - inherit (lib.strings) concatStringsSep repeatString; - inherit (lib.lists) range flatten repeat; + inherit (lib.strings) concatStringsSep; + inherit (extendedLib.strings) repeatString; + + inherit (lib.lists) range flatten; + inherit (extendedLib.lists) repeat; + inherit (lib.attrsets) nameValuePair listToAttrs; nthCds = n: [ @@ -336,7 +337,8 @@ in rec { }; flattened.aliases = let - inherit (lib.attrsets) mapAttrs attrValues filterAttrs isAttrs concatAttrs; + inherit (lib.attrsets) mapAttrs attrValues filterAttrs isAttrs; + inherit (extendedLib.attrsets) concatAttrs; inherit (lib.strings) isString concatStringsSep; applyFunctor = attrset: functors.${attrset.type}.apply attrset; @@ -369,11 +371,12 @@ in rec { xdg.dataFile = { aliases = { text = let - inherit (lib.strings) unlines wrap' replaceStrings' stringLength repeatString; + inherit (lib.strings) stringLength; + inherit (extendedLib.strings) unlines wrap' replaceStrings' repeatString; inherit (lib.attrsets) attrValues mapAttrs isAttrs; inherit (lib.lists) remove; inherit (lib.trivial) mapNullable; - inherit (lib.termColors.front) red green blue; + inherit (extendedLib.termColors.front) red green blue; # int -> String -> AttrSet -> String stringifyCategory = level: name: category: @@ -409,8 +412,8 @@ in rec { packageManagerLecture = { target = "package-manager.lecture"; text = let - inherit (lib.strings) unlines; - inherit (lib.termColors.front) red blue; + inherit (extendedLib.strings) unlines; + inherit (extendedLib.termColors.front) red blue; in unlines [ ((red "This package manager is not installed on ") + (blue "NixOS") + (red ".")) ((red "Either use ") + ("\"nix-env -i\"") + (red " or install it through a configuration file.")) diff --git a/hosts/common.nix b/hosts/common.nix index c116f7f..37288a5 100644 --- a/hosts/common.nix +++ b/hosts/common.nix @@ -1,6 +1,5 @@ -{ pkgs, unstable-pkgs, config, inputs, secrets, ... }: +{ pkgs, unstable-pkgs, lib, extendedLib, config, inputs, secrets, ... }: let - inherit (pkgs) lib; # inherit (specialArgs) machineVars; inherit (config) machineVars; # has_graphics = !config.machineVars.headless; @@ -130,7 +129,7 @@ in { # TODO: move this out of etc, and reference it directly in sudo config. sudoLecture = { target = "sudo.lecture"; - text = lib.termColors.front.red "Be careful or something, idk...\n"; + text = extendedLib.termColors.front.red "Be careful or something, idk...\n"; }; "resolv.conf".source = let diff --git a/overlays/lib/attrsets.nix b/lib/attrsets.nix similarity index 90% rename from overlays/lib/attrsets.nix rename to lib/attrsets.nix index 454f1e3..adfc2ea 100644 --- a/overlays/lib/attrsets.nix +++ b/lib/attrsets.nix @@ -1,8 +1,8 @@ -final: prev: +{ stdlib }: let - inherit (prev.lib.attrsets) mapAttrs isAttrs filterAttrs listToAttrs nameValuePair attrNames mapAttrsToList; - inherit (prev.lib.lists) foldr imap0 imap1; -in prev.lib.attrsets // rec { + inherit (stdlib.attrsets) mapAttrs isAttrs filterAttrs listToAttrs nameValuePair attrNames mapAttrsToList; + inherit (stdlib.lists) foldr imap0 imap1; +in rec { # a -> [String] -> AttrSet{a} mapToAttrsWithConst = constant: items: listToAttrs (map (name: nameValuePair name constant) items); diff --git a/lib/default.nix b/lib/default.nix new file mode 100644 index 0000000..3f3bf1c --- /dev/null +++ b/lib/default.nix @@ -0,0 +1,8 @@ +{ stdlib }: +rec { + attrsets = import ./attrsets.nix { inherit stdlib; }; + lists = import ./lists.nix { inherit stdlib; }; + strings = import ./strings.nix { inherit stdlib lists; }; + termColors = import ./termColors.nix { inherit stdlib strings; }; + trivial = import ./trivial.nix { inherit stdlib; }; +} diff --git a/overlays/lib/lists.nix b/lib/lists.nix similarity index 62% rename from overlays/lib/lists.nix rename to lib/lists.nix index f5cd805..604af97 100644 --- a/overlays/lib/lists.nix +++ b/lib/lists.nix @@ -1,8 +1,8 @@ -final: prev: +{ stdlib }: let - inherit (prev.lib.trivial) const; - inherit (prev.lib.lists) range any all; -in prev.lib.lists // { + inherit (stdlib.trivial) const; + inherit (stdlib.lists) range any all; +in { # a -> Int -> [a] repeat = item: times: map (const item) (range 1 times); diff --git a/overlays/lib/strings.nix b/lib/strings.nix similarity index 83% rename from overlays/lib/strings.nix rename to lib/strings.nix index 4891f45..7031fc2 100644 --- a/overlays/lib/strings.nix +++ b/lib/strings.nix @@ -1,9 +1,9 @@ -final: prev: +{ stdlib, lists }: let - inherit (final.lib.lists) repeat length; - inherit (prev.lib.strings) concatStringsSep replaceStrings splitString; - # inherit (final.lib.strings) wrap; -in prev.lib.strings // rec { + inherit (stdlib.lists) length; + inherit (lists) repeat; + inherit (stdlib.strings) concatStringsSep replaceStrings splitString; +in rec { # String -> [String] lines = splitString "\n"; diff --git a/overlays/lib/termColors.nix b/lib/termColors.nix similarity index 84% rename from overlays/lib/termColors.nix rename to lib/termColors.nix index e04c653..3760ef6 100644 --- a/overlays/lib/termColors.nix +++ b/lib/termColors.nix @@ -1,8 +1,7 @@ -final: prev: +{ stdlib, strings }: let - inherit (final.lib.strings) wrap; - inherit (prev.lib.attrsets) mapAttrs' nameValuePair; - # inherit (final.lib.myStuff.termColors) escapeCharacter escapeColor resetCharacter wrapWithColor' colorMappings; + inherit (strings) wrap; + inherit (stdlib.attrsets) mapAttrs' nameValuePair; in rec { # String escapeCharacter = ""; diff --git a/overlays/lib/trivial.nix b/lib/trivial.nix similarity index 72% rename from overlays/lib/trivial.nix rename to lib/trivial.nix index 54c53b7..0bbb843 100644 --- a/overlays/lib/trivial.nix +++ b/lib/trivial.nix @@ -1,6 +1,5 @@ -final: prev: -let -in prev.lib.trivial // { +{ stdlib }: +{ # a -> b -> Either (a b) withDefault = default: value: if (value == null) then default else value; diff --git a/modules/machineVars.nix b/modules/machineVars.nix index 5c3bde3..57764dd 100644 --- a/modules/machineVars.nix +++ b/modules/machineVars.nix @@ -1,6 +1,5 @@ -{ pkgs, config, ... }: +{ lib, config, ... }: let - inherit (pkgs) lib; inherit (lib) types mkEnableOption mkOption mkIf; cfg = config.machineVars; in { diff --git a/overlays/lib/default.nix b/overlays/lib/default.nix deleted file mode 100644 index 3d356bc..0000000 --- a/overlays/lib/default.nix +++ /dev/null @@ -1,10 +0,0 @@ -final: prev: -{ - lib = prev.lib // { - attrsets = (import ./attrsets.nix) final prev; - lists = (import ./lists.nix) final prev; - strings = (import ./strings.nix) final prev; - termColors = (import ./termColors.nix) final prev; - trivial = (import ./trivial.nix) final prev; - }; -}