Initial commit

This commit is contained in:
James Andariese 2022-10-16 17:46:24 -05:00
commit 68c29cbba1
17 changed files with 145 additions and 0 deletions

7
LICENSE Executable file
View File

@ -0,0 +1,7 @@
Copyright 2022 James Andariese
Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the "Software"), to deal in the Software without restriction, including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so, subject to the following conditions:
The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software.
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.

11
README.md Normal file
View File

@ -0,0 +1,11 @@
# `cascade-functions`
General purpose functions that might be useful.
One file per function. Start with a _ to disable the function being automatically imported.
### Usage
```
with import (fetchGit "https://gitlab.com/jamesandariese/cascade-functions");
```

23
_enrich-machine-attrs.nix Normal file
View File

@ -0,0 +1,23 @@
with builtins;
machine:
let addressParts = match "([0-9.]*)/([0-9]*)" machine.address;
in
if addressParts == null then throw "could not split address into IP and subnet" else
let prefix = fromJSON (elemAt addressParts 1);
ip = head addressParts;
in
machine // {
inherit ip prefix;
interface = {
ipv4.addresses = [{
address = ip;
prefixLength = prefix;
}];
macAddress = machine.mac;
};
}

24
_lookup-machine.nix Normal file
View File

@ -0,0 +1,24 @@
{enrich ? true, interfaceDefinition ? false, ...}@filterSpec:
with builtins;
let
attrList = s: attrValues (mapAttrs (k: v: {name = k; value = v;}) s);
combineAttrs = l: listToAttrs (concatMap attrList l);
t = fromTOML (readFile ../machines.toml);
machines = attrValues (mapAttrs (name: v: {name = name;} // v) t);
filterNullFilters = f: attrValues f != [ null ];
nonNullFilterSpecs = filter filterNullFilters (attrValues (mapAttrs (k: v: {"${k}" = v;}) filterSpec));
filteredFilterSpec = combineAttrs nonNullFilterSpecs;
matchAttrs = fAS: tAS: (all (k: fAS."${k}" == tAS."${k}") (attrNames (intersectAttrs fAS tAS)));
matchingMachines = filter (m: matchAttrs filteredFilterSpec m) machines;
in
if length matchingMachines > 1 then throw "lookup-machine found too many machines" else
if length matchingMachines < 1 then throw "lookup-machine did not find any machines" else
let machine = head matchingMachines; in
let machine = if enrich then import ./enrich-machine-attrs.nix machine else machine; in
machine

5
default.nix Normal file
View File

@ -0,0 +1,5 @@
let _ = builtins.trace "${toString ./.}/default.nix"; in
let import-folder = import (toString ./import-folder.nix);
in
import-folder {path = (toString ./.);}

8
fileLines.nix Normal file
View File

@ -0,0 +1,8 @@
with builtins;
p:
let
contents = readFile p;
splitRes = split "\r?\n" contents;
lines = filter isString splitRes;
in lines

11
flatten.nix Normal file
View File

@ -0,0 +1,11 @@
with builtins;
let flatten-once = foldl' (x: y: x ++ (if isList y then y else [y])) []; # curried. takes a list as final arg.
flatten-many' = flatten-many': l:
let flattened = flatten-once l;
in
if flattened == l
then l
else flatten-many' flatten-many' flattened;
flatten-many = flatten-many' flatten-many';
in
flatten-many

17
import-folder.nix Normal file
View File

@ -0,0 +1,17 @@
with builtins;
let mkIsFilenameAMatch = {filenameMatch ? "[^_].*", filenameBadMatch ? null, ...}@opt:
{name, type, ...}:
if name == "default.nix" then false
else if builtins.match filenameMatch name == null then false
else if filenameBadMatch != null && builtins.match filenameBadMatch name != null then false
else if ! elem type ["symlink" "regular"] then false
else if builtins.match ".*[.]nix" name == null then false
else true;
readDirItems = import ./readDirItems.nix;
extractName = fn: let m = match "(.*[/])?([a-zA-Z0-9-]+)[.]nix" fn; in if m == null then throw "${fn} does not seem to have a correct filename" else head (tail m);
matches = {path, ...}@opt:
let isFilenameAMatch = mkIsFilenameAMatch opt;
in
listToAttrs (map ({name,...}: let pname = name; in {name = "${extractName name}"; value = import "${path}/${name}";}) (filter isFilenameAMatch (readDirItems "${path}" )));
in
matches

1
isMinutessTimeUnit.nix Normal file
View File

@ -0,0 +1 @@
n: if n == null then false else builtins.match "[0-9] *m(in)?" n != null

1
isSecondsTimeUnit.nix Normal file
View File

@ -0,0 +1 @@
n: if n == null then false else builtins.match "[0-9] *(s(ec)?|$)" n != null

12
join-string.nix Normal file
View File

@ -0,0 +1,12 @@
with builtins;
let join' = join': sep: sl:
if sl == []
then ""
else if length sl == 1
then head sl
else "${head sl}${sep}${join' join' sep (tail sl)}";
join = join' join';
in
join

3
match-all-flat.nix Normal file
View File

@ -0,0 +1,3 @@
with import (toString ./.);
with builtins; pattern: s: flatten (match-all pattern s)

1
match-all.nix Normal file
View File

@ -0,0 +1 @@
with builtins; pattern: s: filter isList (split pattern s)

3
mkAttrItemsFunction.nix Normal file
View File

@ -0,0 +1,3 @@
with builtins;
kname: vname: aset: attrValues (mapAttrs (k: v: {"${kname}" = k; "${vname}" = v;}) aset)

4
name-to-mac.nix Normal file
View File

@ -0,0 +1,4 @@
with import (toString ./.);
with builtins;
elts: name: join-string ":" (take elts (match-all-flat "(..)" (hashString "sha256" name)))

3
readDirItems.nix Normal file
View File

@ -0,0 +1,3 @@
with builtins;
p: (import ./mkAttrItemsFunction.nix) "name" "type" (readDir p)

11
take.nix Normal file
View File

@ -0,0 +1,11 @@
with builtins;
let take' = take': n: l:
if n < 0 then throw "take requires a positive whole number or 0." else
if n == 0 || l == []
then []
else [ (head l) ] ++ (take' take' (n - 1) (tail l));
take = take' take';
in
take