lib: move lib extensions into its own module argument

main
Oystein Kristoffer Tveit 2022-11-19 16:14:18 +01:00
parent 9bc5d2e85d
commit b3aa6cad81
Signed by: oysteikt
GPG Key ID: 9F2F7D8250F35146
13 changed files with 70 additions and 83 deletions

View File

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

View File

@ -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)
];

View File

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

View File

@ -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."))

View File

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

View File

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

8
lib/default.nix Normal file
View File

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

View File

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

View File

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

View File

@ -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 = "";

View File

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

View File

@ -1,6 +1,5 @@
{ pkgs, config, ... }:
{ lib, config, ... }:
let
inherit (pkgs) lib;
inherit (lib) types mkEnableOption mkOption mkIf;
cfg = config.machineVars;
in {

View File

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