Skip to content

Commit c83dc7a

Browse files
committed
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`.
1 parent abe673d commit c83dc7a

File tree

2 files changed

+50
-46
lines changed

2 files changed

+50
-46
lines changed

default.nix

Lines changed: 3 additions & 46 deletions
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,9 @@
44

55
with pkgs.haskell.lib;
66

7-
let inherit (pkgs) lib; in rec {
7+
let nixThunkLib = import ./lib.nix { inherit (pkgs) lib; }; in rec {
8+
inherit (nixThunkLib) thunkSource mapSubdirectories;
9+
810
# The version of nixpkgs that we use for fetching packing thunks (by
911
# themselves). Not to be used for building packages.
1012
packedThunkNixpkgs = builtins.fetchTarball {
@@ -107,51 +109,6 @@ let inherit (pkgs) lib; in rec {
107109

108110
inherit (import ./dep/gitignore.nix { inherit lib; }) gitignoreSource;
109111

110-
# Retrieve source that is controlled by the hack-* scripts; it may be either a stub or a checked-out git repo
111-
thunkSource = p:
112-
let
113-
contents = builtins.readDir p;
114-
115-
contentsMatch = { required, optional }:
116-
(let all = required // optional; in all // contents == all)
117-
&& builtins.intersectAttrs required contents == required;
118-
119-
# Newer obelisk thunks include the feature of hackGet with a thunk.nix file in the thunk.
120-
isObeliskThunkWithThunkNix =
121-
let
122-
packed = jsonFileName: {
123-
required = { ${jsonFileName} = "regular"; "default.nix" = "regular"; "thunk.nix" = "regular"; };
124-
optional = { ".attr-cache" = "directory"; };
125-
};
126-
in builtins.any (n: contentsMatch (packed n)) [ "git.json" "github.json" ];
127-
128-
filterArgs = x: removeAttrs x [ "branch" ];
129-
hasValidThunk = name: if builtins.pathExists (p + ("/" + name))
130-
then
131-
contentsMatch {
132-
required = { ${name} = "regular"; };
133-
optional = { "default.nix" = "regular"; ".attr-cache" = "directory"; };
134-
}
135-
|| 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)."
136-
else false;
137-
in
138-
if isObeliskThunkWithThunkNix then import (p + "/thunk.nix")
139-
else if hasValidThunk "git.json" then (
140-
let gitArgs = filterArgs (builtins.fromJSON (builtins.readFile (p + "/git.json")));
141-
in if builtins.elem "@" (lib.stringToCharacters gitArgs.url)
142-
then pkgs.fetchgitPrivate gitArgs
143-
else pkgs.fetchgit gitArgs
144-
)
145-
else if hasValidThunk "github.json" then
146-
pkgs.fetchFromGitHub (filterArgs (builtins.fromJSON (builtins.readFile (p + "/github.json"))))
147-
else {
148-
name = baseNameOf p;
149-
outPath = gitignoreSource p;
150-
};
151-
152-
#TODO: This really shouldn't include *all* symlinks, just ones that point at directories
153-
mapSubdirectories = f: dir: lib.mapAttrs (name: _: f (dir + "/${name}")) (lib.filterAttrs (_: type: type == "directory" || type == "symlink") (builtins.readDir dir));
154-
155112
##############################################################################
156113
# Deprecated functions
157114
##############################################################################

lib.nix

Lines changed: 47 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,47 @@
1+
{ lib }:
2+
rec {
3+
# Retrieve source that is controlled by the hack-* scripts; it may be either a stub or a checked-out git repo
4+
thunkSource = p:
5+
let
6+
contents = builtins.readDir p;
7+
8+
contentsMatch = { required, optional }:
9+
(let all = required // optional; in all // contents == all)
10+
&& builtins.intersectAttrs required contents == required;
11+
12+
# Newer obelisk thunks include the feature of hackGet with a thunk.nix file in the thunk.
13+
isObeliskThunkWithThunkNix =
14+
let
15+
packed = jsonFileName: {
16+
required = { ${jsonFileName} = "regular"; "default.nix" = "regular"; "thunk.nix" = "regular"; };
17+
optional = { ".attr-cache" = "directory"; };
18+
};
19+
in builtins.any (n: contentsMatch (packed n)) [ "git.json" "github.json" ];
20+
21+
filterArgs = x: removeAttrs x [ "branch" ];
22+
hasValidThunk = name: if builtins.pathExists (p + ("/" + name))
23+
then
24+
contentsMatch {
25+
required = { ${name} = "regular"; };
26+
optional = { "default.nix" = "regular"; ".attr-cache" = "directory"; };
27+
}
28+
|| 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)."
29+
else false;
30+
in
31+
if isObeliskThunkWithThunkNix then import (p + "/thunk.nix")
32+
else if hasValidThunk "git.json" then (
33+
let gitArgs = filterArgs (builtins.fromJSON (builtins.readFile (p + "/git.json")));
34+
in if builtins.elem "@" (lib.stringToCharacters gitArgs.url)
35+
then pkgs.fetchgitPrivate gitArgs
36+
else pkgs.fetchgit gitArgs
37+
)
38+
else if hasValidThunk "github.json" then
39+
pkgs.fetchFromGitHub (filterArgs (builtins.fromJSON (builtins.readFile (p + "/github.json"))))
40+
else {
41+
name = baseNameOf p;
42+
outPath = gitignoreSource p;
43+
};
44+
45+
#TODO: This really shouldn't include *all* symlinks, just ones that point at directories
46+
mapSubdirectories = f: dir: lib.mapAttrs (name: _: f (dir + "/${name}")) (lib.filterAttrs (_: type: type == "directory" || type == "symlink") (builtins.readDir dir));
47+
}

0 commit comments

Comments
 (0)