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 = {
nixpkgsLib.url = "github:divnix/nixpkgs.lib";
};
outputs = { self, nixpkgsLib }:
outputs = { self }:
let
lib = nixpkgsLib.lib // {
recursiveAttrPaths = set:
let
flattenIfHasList = x:
if (lib.isList x) && (lib.any lib.isList x)
then lib.concatMap flattenIfHasList x
else [ x ];
utils = import ./utils.nix;
recurse = path: set:
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; };
tags = import ./tags.nix { format = false; inherit utils; };
pkgsLib = (final: prev: {
htmlNix = import ./pkgs-lib.nix { inherit lib; pkgs = prev; };
htmlNix = import ./pkgs-lib.nix { pkgs = prev; inherit utils; };
});
in
{

View File

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

View File

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