-
Notifications
You must be signed in to change notification settings - Fork 9
Expand file tree
/
Copy pathdefault.nix
More file actions
97 lines (90 loc) · 3.54 KB
/
default.nix
File metadata and controls
97 lines (90 loc) · 3.54 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
# This is the wallpkgs... un-extended library. It appends a few useful functions
# based on their nixpkgs.lib variants, without any reliance on nixpkgs' own lib.
# Idea is that we do not need to pull an input for nixpkgs library alone, since
# wallpapers here are no longer packages. If implementing a new function, please
# remember to keep it lib-free. If it *must* rely on lib, then implement the
# function or functions you need as well as the main function.
let
splitString = separator: str: let
# "Nix doesn't have for loops it's a functional programming language!"
# You know what it has now? For loops. Nerd.
loop = str: acc: currentPos: currentSegment:
if currentPos == builtins.stringLength str
then
# end of string: add the current segment (even if it's empty)
acc ++ [currentSegment]
else let
char = builtins.substring currentPos 1 str;
in
if char == separator
then
# separator; add the current segment (even if it's empty) to acc
# and start a new segment for the next part
loop str (acc ++ [currentSegment]) (currentPos + 1) ""
else loop str acc (currentPos + 1) (currentSegment + char);
in
loop str [] 0 "";
filterAttrs = f: attrs: let
names = builtins.attrNames attrs;
filteredNames = builtins.filter (name: f name (attrs.${name})) names;
in
builtins.listToAttrs (map (name: {
name = name;
value = attrs.${name};
})
filteredNames);
genAttrs = names: f:
builtins.listToAttrs (map (name: {
name = name;
value = f name;
})
names);
# Yoinked from nixpkgs
hasSuffix = suffix: content: let
lenContent = builtins.stringLength content;
lenSuffix = builtins.stringLength suffix;
in (
lenContent >= lenSuffix && builtins.substring (lenContent - lenSuffix) lenContent content == suffix
);
concatMapAttrs = f: attrs: builtins.foldl' (it: ac: it // ac) {} (
builtins.attrValues (
builtins.mapAttrs f attrs
)
);
# TODO: This needs to be extensible, possibly in order to allow additional directories.
# In theory, we should only need to handle path*s* instead of a path, and search multiple
# paths by extension instead of just once, right?
toWallpkgs = path: extensions: let
hasValidExtension = file: builtins.foldl' (acc: elem: (hasSuffix elem "${file}") || acc) false extensions;
fetchPath = path:
filterAttrs (n: t: t == "directory" || (t == "regular" && hasValidExtension n)) (
builtins.readDir path
);
getFiles = path:
concatMapAttrs (
n: v:
if v == "directory"
then {
${n} = getFiles "${path}/${n}";
}
else let
name = builtins.head (splitString "." n);
in {
${name} = {
path = "${path}/${n}";
tags = splitString "-" name;
# in theory, md5 is the fastest option because it produces a 128-bit hash instead of >= 160
hash = builtins.hashFile "md5" "${path}/${n}";
};
}
) (fetchPath path);
in
getFiles path;
in {
# Partial re-implementations of functions from Nixpkgs.
# They may not work as intended, as the implementation is *completely* different. If this is
# the case while contributing, please create an issue!
inherit splitString filterAttrs genAttrs;
# Main function to create the final collection of wallpapers from a given directory.
inherit toWallpkgs;
}