commit 68c29cbba1587f550e8825cd9992a0ccc9994640 Author: James Andariese Date: Sun Oct 16 17:46:24 2022 -0500 Initial commit diff --git a/LICENSE b/LICENSE new file mode 100755 index 0000000..edd2349 --- /dev/null +++ b/LICENSE @@ -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. diff --git a/README.md b/README.md new file mode 100644 index 0000000..f965896 --- /dev/null +++ b/README.md @@ -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"); +``` diff --git a/_enrich-machine-attrs.nix b/_enrich-machine-attrs.nix new file mode 100644 index 0000000..1f0aada --- /dev/null +++ b/_enrich-machine-attrs.nix @@ -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; + }; +} diff --git a/_lookup-machine.nix b/_lookup-machine.nix new file mode 100644 index 0000000..5cb2e33 --- /dev/null +++ b/_lookup-machine.nix @@ -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 diff --git a/default.nix b/default.nix new file mode 100644 index 0000000..a94bd83 --- /dev/null +++ b/default.nix @@ -0,0 +1,5 @@ +let _ = builtins.trace "${toString ./.}/default.nix"; in + +let import-folder = import (toString ./import-folder.nix); +in + import-folder {path = (toString ./.);} diff --git a/fileLines.nix b/fileLines.nix new file mode 100644 index 0000000..f3c9215 --- /dev/null +++ b/fileLines.nix @@ -0,0 +1,8 @@ +with builtins; + +p: + let + contents = readFile p; + splitRes = split "\r?\n" contents; + lines = filter isString splitRes; + in lines diff --git a/flatten.nix b/flatten.nix new file mode 100644 index 0000000..0582102 --- /dev/null +++ b/flatten.nix @@ -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 diff --git a/import-folder.nix b/import-folder.nix new file mode 100644 index 0000000..9c4d8e2 --- /dev/null +++ b/import-folder.nix @@ -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 diff --git a/isMinutessTimeUnit.nix b/isMinutessTimeUnit.nix new file mode 100644 index 0000000..de81468 --- /dev/null +++ b/isMinutessTimeUnit.nix @@ -0,0 +1 @@ +n: if n == null then false else builtins.match "[0-9] *m(in)?" n != null diff --git a/isSecondsTimeUnit.nix b/isSecondsTimeUnit.nix new file mode 100644 index 0000000..2f853db --- /dev/null +++ b/isSecondsTimeUnit.nix @@ -0,0 +1 @@ +n: if n == null then false else builtins.match "[0-9] *(s(ec)?|$)" n != null diff --git a/join-string.nix b/join-string.nix new file mode 100644 index 0000000..963cb24 --- /dev/null +++ b/join-string.nix @@ -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 diff --git a/match-all-flat.nix b/match-all-flat.nix new file mode 100644 index 0000000..8acb11d --- /dev/null +++ b/match-all-flat.nix @@ -0,0 +1,3 @@ +with import (toString ./.); + +with builtins; pattern: s: flatten (match-all pattern s) diff --git a/match-all.nix b/match-all.nix new file mode 100644 index 0000000..94db927 --- /dev/null +++ b/match-all.nix @@ -0,0 +1 @@ +with builtins; pattern: s: filter isList (split pattern s) diff --git a/mkAttrItemsFunction.nix b/mkAttrItemsFunction.nix new file mode 100644 index 0000000..afea593 --- /dev/null +++ b/mkAttrItemsFunction.nix @@ -0,0 +1,3 @@ +with builtins; + +kname: vname: aset: attrValues (mapAttrs (k: v: {"${kname}" = k; "${vname}" = v;}) aset) diff --git a/name-to-mac.nix b/name-to-mac.nix new file mode 100644 index 0000000..dc85899 --- /dev/null +++ b/name-to-mac.nix @@ -0,0 +1,4 @@ +with import (toString ./.); +with builtins; + +elts: name: join-string ":" (take elts (match-all-flat "(..)" (hashString "sha256" name))) diff --git a/readDirItems.nix b/readDirItems.nix new file mode 100644 index 0000000..1a7390d --- /dev/null +++ b/readDirItems.nix @@ -0,0 +1,3 @@ +with builtins; + +p: (import ./mkAttrItemsFunction.nix) "name" "type" (readDir p) diff --git a/take.nix b/take.nix new file mode 100644 index 0000000..aafb14d --- /dev/null +++ b/take.nix @@ -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