-
Notifications
You must be signed in to change notification settings - Fork 0
/
WallpaperManager.nix
204 lines (180 loc) · 4.09 KB
/
WallpaperManager.nix
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
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
let
pkgs=(import <nixpkgs>{});
# wallpaperOpts should have the following format:
#{
# settings =
# {
# setterProgram = {
# executable = "${pkgs.feh}/bin/feh";
# arguments = [ "--bg-max" "--no-xinerama" ];
# };
# # This is the output hash for fixed output derivation
# hash = "0000000000000000000000000000000000000000000000000000000000000000";
# };
# wallpapers = {
# urls = [
# "url 1"
# "url 2"
# # Mentioning hash prevents redownload of
# # all wallpapers everytime a
# # single is added or deleted
# { url = "url3"; hash = "hash3";}
# ];
# localFiles = [ ... ] ;
# localDirs = [ ... ] ;
# };
#}
addNullHashtoUrls =
urlWithOptionalHash:
if
( builtins.typeOf urlWithOptionalHash == "string" )
then
{
url = urlWithOptionalHash;
hash = "";
}
else
urlWithOptionalHash
;
imageFileFilter = imageFileName:
(
pkgs.lib.hasSuffix "png" imageFileName
|| pkgs.lib.hasSuffix "jpg" imageFileName
)
;
wallpaperOpts = (import ./WallpaperOpts.nix { inherit pkgs; } );
backgroundSetterProgram = wallpaperOpts.settings.setterProgram.executable;
backgroundSetterArguments = wallpaperOpts.settings.setterProgram.arguments;
backgroundSetterArgumentsSingleString = pkgs.lib.concatStringsSep " " backgroundSetterArguments;
allWallpapers = wallpaperOpts.wallpapers;
allUrlWallpapers =
builtins.map
addNullHashtoUrls
allWallpapers.urls
;
singleDirectoryWallpapers =
dirName:
(
builtins.filter
imageFileFilter
(
pkgs.lib.mapAttrsToList
( name: value: name )
( builtins.readDir dirName )
)
)
;
allDirectoryWallpapers =
pkgs.lib.flatten
(
map
singleDirectoryWallpapers
allWallpapers.localDirs
)
;
nix_code_prologue = ''
echo "pkgs: " > $1
echo "[" >> $1
'';
nix_code_generator_script =
pkgs.lib.concatStringsSep
"\n"
(
map
(
{
url,
hash
}:
''
if [ -z "${hash}" ]
then
${pkgs.wget}/bin/wget --no-check-certificate ${url} -O blah 2>/dev/null
hash=$(${pkgs.nix}/bin/nix-hash --type sha256 --flat --base32 blah)
echo ${url}
echo $hash
else
#echo "hash ${hash} already known for ${url}"
hash="${hash}"
fi
if [ ! -z $hash ]
then
cat << XJK >> $1
(
pkgs.fetchurl{
url = "${url}";
sha256="$hash";
}
)
XJK
else
echo "========hash FAILED for ${url}============="
fi
''
)
allUrlWallpapers
)
;
nix_code_epilogue = ''
echo "]" >> $1
echo "" >> $1
'';
bashCreatorOfWallpaperFetcherNixScript = (
pkgs.writeShellScript
"BashCreatorOfWallpaperFetcherNixScript"
(nix_code_prologue nix_code_generator_script nix_code_epilogue)
);
wallpaperFetcherNixScript = (
pkgs.stdenv.mkDerivation
(
{
name = "wallpaper-fetcher.nix";
buildCommand =
''
${bashCreatorOfWallpaperFetcherNixScript} $out
''
;
}
//
(
if (
builtins.all ( url: (builtins.typeOf url) != "string" ) wallpaperOpts.wallpapers.urls
)
then
{}
else
{
outputHashAlgo = "sha256";
outputHashMode = "recursive";
outputHash = wallpaperOpts.settings.hash;
}
)
)
);
downloadedOnlineWallpapers = (import wallpaperFetcherNixScript pkgs);
allWallpapersLocalized = downloadedOnlineWallpapers allWallpapers.localFiles allDirectoryWallpapers;
numWallpapers =
builtins.toString
(
builtins.length
allWallpapersLocalized
)
;
wallpaperBashArray =
"wallpaperArray=(\n"
(
pkgs.lib.concatStringsSep
"\n"
allWallpapersLocalized
)
"\n)"
;
in
pkgs.writeShellScript
"RandomWallpaperSetter"
(
wallpaperBashArray "\n"
"chosenWallpaper=$" "{wallpaperArray[$(( $RANDOM % ${numWallpapers} ))]}\n"
"echo $chosenWallpaper\n"
"${backgroundSetterProgram} ${backgroundSetterArgumentsSingleString} $chosenWallpaper\n"
)