From d8394373ec453d51a3cfdb90f9bf1104b8e485a2 Mon Sep 17 00:00:00 2001 From: sstent Date: Tue, 28 Feb 2023 12:56:08 +0000 Subject: [PATCH] sync --- flake.lock | 34 --------------------- flake.nix | 17 ++++++----- home-manager/global/keybase.nix | 48 ++--------------------------- lib/attrs.nix | 26 ++++++++++++++++ lib/default.nix | 44 ++++++++++++++++++++------- lib/generators.nix | 30 +++++++++++++++++++ lib/modules.nix | 53 +++++++++++++++++++++++++++++++++ lib/nixos.nix | 25 ++++++++++++++++ lib/options.nix | 18 +++++++++++ 9 files changed, 197 insertions(+), 98 deletions(-) create mode 100644 lib/attrs.nix create mode 100644 lib/generators.nix create mode 100644 lib/modules.nix create mode 100644 lib/nixos.nix create mode 100644 lib/options.nix diff --git a/flake.lock b/flake.lock index 4f1a787..a29097f 100644 --- a/flake.lock +++ b/flake.lock @@ -61,21 +61,6 @@ "type": "github" } }, - "flake-utils_3": { - "locked": { - "lastModified": 1644229661, - "narHash": "sha256-1YdnJAsNy69bpcjuoKdOYQX0YxZBiCYZo4Twxerqv7k=", - "owner": "numtide", - "repo": "flake-utils", - "rev": "3cecb5b042f7f209c56ffd8371b2711a290ec797", - "type": "github" - }, - "original": { - "owner": "numtide", - "repo": "flake-utils", - "type": "github" - } - }, "home-manager": { "inputs": { "nixpkgs": [ @@ -261,7 +246,6 @@ "nixpkgs": "nixpkgs", "nixpkgs-unstable": "nixpkgs-unstable", "sops-nix": "sops-nix", - "utils": "utils_2", "vscode-server": "vscode-server" } }, @@ -299,24 +283,6 @@ "type": "github" } }, - "utils_2": { - "inputs": { - "flake-utils": "flake-utils_3" - }, - "locked": { - "lastModified": 1657226504, - "narHash": "sha256-GIYNjuq4mJlFgqKsZ+YrgzWm0IpA4axA3MCrdKYj7gs=", - "owner": "gytis-ivaskevicius", - "repo": "flake-utils-plus", - "rev": "2bf0f91643c2e5ae38c1b26893ac2927ac9bd82a", - "type": "github" - }, - "original": { - "owner": "gytis-ivaskevicius", - "repo": "flake-utils-plus", - "type": "github" - } - }, "vscode-server": { "inputs": { "nixpkgs": "nixpkgs_3" diff --git a/flake.nix b/flake.nix index 9312f21..32b352d 100644 --- a/flake.nix +++ b/flake.nix @@ -28,7 +28,6 @@ vscode-server.url = "github:msteen/nixos-vscode-server"; # homeage = {url = "github:jordanisaacs/homeage"; inputs.nixpkgs.follows = "nixpkgs";}; - # Nix Library Functions library = { type = "github"; owner = "nix-community"; @@ -36,27 +35,29 @@ ref = "master"; rev = "af5239f892ae6e1c8bb560b11ed874cebbd10696"; }; - # Source Filter Functions + filter.url = "github:numtide/nix-filter"; ignore = { url = "github:hercules-ci/gitignore.nix"; inputs.nixpkgs.follows = "nixpkgs"; }; - # Flake Utility Functions - utils.url = "github:gytis-ivaskevicius/flake-utils-plus"; - }; - outputs = { self, nixpkgs, home-manager,library, ignore, filter,... }@inputs: + outputs = { self, nixpkgs, home-manager, ignore, filter, library,... }@inputs: let inherit (self) outputs; + inherit (lib.my) mapModules mapModulesRec mapHosts; + forEachSystem = nixpkgs.lib.genAttrs [ "x86_64-linux" "aarch64-linux" ]; platforms = ["x86_64-linux"]; forEachPkgs = f: forEachSystem (sys: f nixpkgs.legacyPackages.${sys}); + lib = nixpkgs.lib.extend + (self: super: { my = import ./lib { inherit nixpkgs inputs; lib = self; }; }); + in rec { packages = forEachPkgs (pkgs: import ./pkgs { inherit pkgs; }); @@ -65,7 +66,7 @@ nixosModules = import ./modules/nixos; homeManagerModules = import ./modules/home-manager; - util = import ./lib/map.nix; + lib = lib.my; # NixOS configuration entrypoint # Available through 'nixos-rebuild --flake .#your-hostname' @@ -91,7 +92,7 @@ # FIXME replace with your username@hostname "sstent@Go3" = home-manager.lib.homeManagerConfiguration { pkgs = nixpkgs.legacyPackages.x86_64-linux; # Home-manager requires 'pkgs' instance - extraSpecialArgs = { inherit inputs outputs; hostName = "Go3"; }; + extraSpecialArgs = { inherit inputs outputs lib; hostName = "Go3"; }; modules = [ # > Our main home-manager configuration file < ./home-manager/users/sstent diff --git a/home-manager/global/keybase.nix b/home-manager/global/keybase.nix index 0e5fd74..62ccc3f 100644 --- a/home-manager/global/keybase.nix +++ b/home-manager/global/keybase.nix @@ -1,53 +1,11 @@ -{ config, pkgs, inputs, lib, hostName, util, ... }: +{ config, pkgs, lib, inputs, hostName, outputs, ... }: with lib; +with lib.my; let - inherit - (builtins) - # attrNames - # attrValues - # foldl' - # isPath - # pathExists - readDir - # toString - ; - - inherit - (lib) - # flatten - filterAttrs - # forEach - # getAttrFromPath - # hasPrefix - # hasSuffix - # id - # mapAttrs' - # mapAttrsToList - # mkIf - # nameValuePair - # removeSuffix - ; cfg = config.custom.keybase; username = config.home.username; secretstore = config._secretstore; -# map = import "${inputs.self}/lib/map.nix"; - - # 'sops' Encrypted Secrets - hm_secrets = dir: out_dir: - filter (name: type: type != null && !(hasPrefix "_" name)) (name: type: - if type == "regular" - then - nameValuePair name { - sopsFile = dir + "/${name}"; - format = "binary"; - path = out_dir + "/${name}"; - - } - else nameValuePair "" null) (readDir dir); - - filter = name: func: attrs: filterAttrs name (mapAttrs' func attrs); - in { # imports = [ @@ -70,7 +28,7 @@ in { systemd.user.services.kbfs.Unit.After = [ "sops-nix.service" ]; sops = { - secrets = hm_secrets "${secretstore}/user_dotfiles/${username}@${hostName}/keybase/" "${config.xdg.configHome}/keybase/"; + secrets = map.hm_secrets "${secretstore}/user_dotfiles/${username}@${hostName}/keybase/" "${config.xdg.configHome}/keybase/"; }; }; } diff --git a/lib/attrs.nix b/lib/attrs.nix new file mode 100644 index 0000000..0f8ebd1 --- /dev/null +++ b/lib/attrs.nix @@ -0,0 +1,26 @@ +{ lib, ... }: + +with builtins; +with lib; +rec { + # attrsToList + attrsToList = attrs: + mapAttrsToList (name: value: { inherit name value; }) attrs; + + # mapFilterAttrs :: + # (name -> value -> bool) + # (name -> value -> { name = any; value = any; }) + # attrs + mapFilterAttrs = pred: f: attrs: filterAttrs pred (mapAttrs' f attrs); + + # Generate an attribute set by mapping a function over a list of values. + genAttrs' = values: f: listToAttrs (map f values); + + # anyAttrs :: (name -> value -> bool) attrs + anyAttrs = pred: attrs: + any (attr: pred attr.name attr.value) (attrsToList attrs); + + # countAttrs :: (name -> value -> bool) attrs + countAttrs = pred: attrs: + count (attr: pred attr.name attr.value) (attrsToList attrs); +} diff --git a/lib/default.nix b/lib/default.nix index d21527e..564d6bd 100644 --- a/lib/default.nix +++ b/lib/default.nix @@ -1,11 +1,33 @@ -{ - self, - platforms, - lib, -} @ args: -with {inherit (lib) makeExtensible attrValues foldr;}; - (makeExtensible (final: - with final; - (import ./map.nix args).modules ./. (file: import file args))) - .extend - (final: prev: foldr (x: y: x // y) {} (attrValues prev)) \ No newline at end of file +# {lib}: +# lib.makeExtensible (self: +# let +# callLibs = file: import file { lib = self; }; +# in +# rec { +# ## Define your own library functions here! +# #id = x: x; +# ## Or in files, containing functions that take {lib} +# map = callLibs ./map.nix; +# ## In configs, they can be used under "lib.our" +# }) + + + +{ inputs, lib, pkgs, ... }: + +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)) \ No newline at end of file diff --git a/lib/generators.nix b/lib/generators.nix new file mode 100644 index 0000000..4b298e5 --- /dev/null +++ b/lib/generators.nix @@ -0,0 +1,30 @@ +{ lib, pkgs, ... }: + +with builtins; +with lib; +{ + toCSSFile = file: + let fileName = removeSuffix ".scss" (baseNameOf file); + compiledStyles = + pkgs.runCommand "compileScssFile" + { buildInputs = [ pkgs.sass ]; } '' + mkdir "$out" + scss --sourcemap=none \ + --no-cache \ + --style compressed \ + --default-encoding utf-8 \ + "${file}" \ + >>"$out/${fileName}.css" + ''; + in "${compiledStyles}/${fileName}.css"; + + toFilteredImage = imageFile: options: + let result = "result.png"; + filteredImage = + pkgs.runCommand "filterWallpaper" + { buildInputs = [ pkgs.imagemagick ]; } '' + mkdir "$out" + convert ${options} ${imageFile} $out/${result} + ''; + in "${filteredImage}/${result}"; +} diff --git a/lib/modules.nix b/lib/modules.nix new file mode 100644 index 0000000..47743f1 --- /dev/null +++ b/lib/modules.nix @@ -0,0 +1,53 @@ +{ self, lib, ... }: + +let + inherit (builtins) attrValues readDir pathExists concatLists; + inherit (lib) id mapAttrsToList filterAttrs hasPrefix hasSuffix nameValuePair removeSuffix; + inherit (self.attrs) mapFilterAttrs; +in +rec { + 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); + + mapModules' = dir: fn: + attrValues (mapModules dir fn); + + mapModulesRec = dir: fn: + mapFilterAttrs + (n: v: + v != null && + !(hasPrefix "_" n)) + (n: v: + let path = "${toString dir}/${n}"; in + if v == "directory" + then nameValuePair n (mapModulesRec path fn) + else if v == "regular" && n != "default.nix" && hasSuffix ".nix" n + then nameValuePair (removeSuffix ".nix" n) (fn path) + else nameValuePair "" null) + (readDir dir); + + mapModulesRec' = dir: fn: + let + dirs = + mapAttrsToList + (k: _: "${dir}/${k}") + (filterAttrs + (n: v: v == "directory" && !(hasPrefix "_" n)) + (readDir dir)); + files = attrValues (mapModules dir id); + paths = files ++ concatLists (map (d: mapModulesRec' d id) dirs); + in map fn paths; +} diff --git a/lib/nixos.nix b/lib/nixos.nix new file mode 100644 index 0000000..4595af7 --- /dev/null +++ b/lib/nixos.nix @@ -0,0 +1,25 @@ +{ inputs, lib, pkgs, ... }: + +with lib; +with lib.my; +let sys = "x86_64-linux"; +in { + mkHost = path: attrs @ { system ? sys, ... }: + nixosSystem { + inherit system; + specialArgs = { inherit lib inputs system; }; + modules = [ + { + nixpkgs.pkgs = pkgs; + networking.hostName = mkDefault (removeSuffix ".nix" (baseNameOf path)); + } + (filterAttrs (n: v: !elem n [ "system" ]) attrs) + ../. # /default.nix + (import path) + ]; + }; + + mapHosts = dir: attrs @ { system ? system, ... }: + mapModules dir + (hostPath: mkHost hostPath attrs); +} diff --git a/lib/options.nix b/lib/options.nix new file mode 100644 index 0000000..2b9c081 --- /dev/null +++ b/lib/options.nix @@ -0,0 +1,18 @@ +{ lib, ... }: + +let + inherit (lib) mkOption types; +in +rec { + mkOpt = type: default: + mkOption { inherit type default; }; + + mkOpt' = type: default: description: + mkOption { inherit type default description; }; + + mkBoolOpt = default: mkOption { + inherit default; + type = types.bool; + example = true; + }; +}