-
Notifications
You must be signed in to change notification settings - Fork 11
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Split utility functions out from default.nix
nix-thunk has two consumers: those who want to build the tool as such, and those who want to use its nix functions to manipulate thunks. The latter have been split out into `lib.nix`.
- Loading branch information
1 parent
abe673d
commit c83dc7a
Showing
2 changed files
with
50 additions
and
46 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,47 @@ | ||
{ lib }: | ||
rec { | ||
# Retrieve source that is controlled by the hack-* scripts; it may be either a stub or a checked-out git repo | ||
thunkSource = p: | ||
let | ||
contents = builtins.readDir p; | ||
|
||
contentsMatch = { required, optional }: | ||
(let all = required // optional; in all // contents == all) | ||
&& builtins.intersectAttrs required contents == required; | ||
|
||
# Newer obelisk thunks include the feature of hackGet with a thunk.nix file in the thunk. | ||
isObeliskThunkWithThunkNix = | ||
let | ||
packed = jsonFileName: { | ||
required = { ${jsonFileName} = "regular"; "default.nix" = "regular"; "thunk.nix" = "regular"; }; | ||
optional = { ".attr-cache" = "directory"; }; | ||
}; | ||
in builtins.any (n: contentsMatch (packed n)) [ "git.json" "github.json" ]; | ||
|
||
filterArgs = x: removeAttrs x [ "branch" ]; | ||
hasValidThunk = name: if builtins.pathExists (p + ("/" + name)) | ||
then | ||
contentsMatch { | ||
required = { ${name} = "regular"; }; | ||
optional = { "default.nix" = "regular"; ".attr-cache" = "directory"; }; | ||
} | ||
|| throw "Thunk at ${toString p} has files in addition to ${name} and optionally default.nix and .attr-cache. Remove either ${name} or those other files to continue (check for leftover .git too)." | ||
else false; | ||
in | ||
if isObeliskThunkWithThunkNix then import (p + "/thunk.nix") | ||
else if hasValidThunk "git.json" then ( | ||
let gitArgs = filterArgs (builtins.fromJSON (builtins.readFile (p + "/git.json"))); | ||
in if builtins.elem "@" (lib.stringToCharacters gitArgs.url) | ||
then pkgs.fetchgitPrivate gitArgs | ||
else pkgs.fetchgit gitArgs | ||
) | ||
else if hasValidThunk "github.json" then | ||
pkgs.fetchFromGitHub (filterArgs (builtins.fromJSON (builtins.readFile (p + "/github.json")))) | ||
else { | ||
name = baseNameOf p; | ||
outPath = gitignoreSource p; | ||
}; | ||
|
||
#TODO: This really shouldn't include *all* symlinks, just ones that point at directories | ||
mapSubdirectories = f: dir: lib.mapAttrs (name: _: f (dir + "/${name}")) (lib.filterAttrs (_: type: type == "directory" || type == "symlink") (builtins.readDir dir)); | ||
} |