refactor: eh maybe we dont really need to depend on nixpkgs lib

This commit is contained in:
dusk 2021-05-15 04:11:35 +03:00
parent 4363f4de53
commit 947b755595
Signed by: dusk
GPG Key ID: 1D8F8FAF2294D6EA
5 changed files with 46 additions and 62 deletions

View File

@ -1,26 +0,0 @@
{
"nodes": {
"nixpkgsLib": {
"locked": {
"lastModified": 1620519687,
"narHash": "sha256-+6Dd72b2CASuXm2W7KRxZIE7AOy/dj4mU28vaF+zxcs=",
"owner": "divnix",
"repo": "nixpkgs.lib",
"rev": "c7b6169809c5f74dd0c34f3d69e9d12ba4d448de",
"type": "github"
},
"original": {
"owner": "divnix",
"repo": "nixpkgs.lib",
"type": "github"
}
},
"root": {
"inputs": {
"nixpkgsLib": "nixpkgsLib"
}
}
},
"root": "root",
"version": 7
}

View File

@ -1,34 +1,11 @@
{ {
inputs = { outputs = { self }:
nixpkgsLib.url = "github:divnix/nixpkgs.lib";
};
outputs = { self, nixpkgsLib }:
let let
lib = nixpkgsLib.lib // { utils = import ./utils.nix;
recursiveAttrPaths = set:
let
flattenIfHasList = x:
if (lib.isList x) && (lib.any lib.isList x)
then lib.concatMap flattenIfHasList x
else [ x ];
recurse = path: set: tags = import ./tags.nix { format = false; inherit utils; };
let
g =
name: value:
if lib.isAttrs value
then recurse (path ++ [ name ]) value
else path ++ [ name ];
in
lib.mapAttrsToList g set;
in
flattenIfHasList (recurse [ ] set);
};
tags = import ./tags.nix { format = false; inherit lib; };
pkgsLib = (final: prev: { pkgsLib = (final: prev: {
htmlNix = import ./pkgs-lib.nix { inherit lib; pkgs = prev; }; htmlNix = import ./pkgs-lib.nix { pkgs = prev; inherit utils; };
}); });
in in
{ {

View File

@ -1,4 +1,4 @@
{ lib, pkgs }: { utils, pkgs }:
let pkgBin = name: "${pkgs.${name}}/bin/${name}"; in let pkgBin = name: "${pkgs.${name}}/bin/${name}"; in
{ {
mkServePathScript = path: pkgs.writeScriptBin "serve" '' mkServePathScript = path: pkgs.writeScriptBin "serve" ''
@ -8,13 +8,16 @@ let pkgBin = name: "${pkgs.${name}}/bin/${name}"; in
mkSitePath = site: mkSitePath = site:
let let
fileAttrPaths = lib.recursiveAttrPaths site; inherit (utils) recursiveAttrPaths concatStringsSep map;
texts = lib.mapAttrsRecursive (path: value: pkgs.writeText (lib.concatStringsSep "-" path) value) site; inherit (pkgs.lib) mapAttrsRecursive init last getAttrFromPath;
mkCreateFileCmd = path: value: let p = lib.concatStringsSep "/" (lib.init path); in "mkdir -p $out/${p} && ln -s ${value} $out/${p}/${lib.last path}";
createFileCmds = map (path: mkCreateFileCmd path (lib.getAttrFromPath path texts)) fileAttrPaths; fileAttrPaths = recursiveAttrPaths site;
texts = mapAttrsRecursive (path: value: pkgs.writeText (concatStringsSep "-" path) value) site;
mkCreateFileCmd = path: value: let p = concatStringsSep "/" (init path); in "mkdir -p $out/${p} && ln -s ${value} $out/${p}/${last path}";
createFileCmds = map (path: mkCreateFileCmd path (getAttrFromPath path texts)) fileAttrPaths;
in in
pkgs.runCommand "site-path" { } '' pkgs.runCommand "site-path" { } ''
mkdir -p $out mkdir -p $out
${lib.concatStringsSep "\n" createFileCmds} ${concatStringsSep "\n" createFileCmds}
''; '';
} }

View File

@ -1,6 +1,6 @@
{ format ? false, lib }: { format ? false, utils }:
let let
inherit (lib) concatStrings mapAttrsToList genAttrs isAttrs isList; inherit (utils) concatStrings mapAttrsToList genAttrs isAttrs isList;
fmt = if format then "\n " else ""; fmt = if format then "\n " else "";
@ -11,7 +11,7 @@ let
then (children: "<${name}${evalAttrs maybeAttrs}>${fmt}${evalChildren children}${fmt}</${name}>") then (children: "<${name}${evalAttrs maybeAttrs}>${fmt}${evalChildren children}${fmt}</${name}>")
else tag name { } maybeAttrs; else tag name { } maybeAttrs;
tags = (genAttrs [ "html" "head" "body" "div" "p" "a" ] tag); tags = (genAttrs tag [ "html" "head" "body" "div" "p" "a" ]);
in in
tags // { tags // {
inherit tag; inherit tag;

30
utils.nix Normal file
View File

@ -0,0 +1,30 @@
let
inherit (builtins) isAttrs isList map any concatMap concatStringsSep listToAttrs;
mapAttrsToList = f: attrs: map (name: f name attrs.${name}) (builtins.attrNames attrs);
in
{
inherit mapAttrsToList;
recursiveAttrPaths = set:
let
flattenIfHasList = x:
if (isList x) && (any isList x)
then concatMap flattenIfHasList x
else [ x ];
recurse = path: set:
let
g =
name: value:
if isAttrs value
then recurse (path ++ [ name ]) value
else path ++ [ name ];
in
mapAttrsToList g set;
in
flattenIfHasList (recurse [ ] set);
concatStrings = concatStringsSep "";
genAttrs = f: names: listToAttrs (map (n: { name = n; value = (f n); }) names);
} // builtins