-
Notifications
You must be signed in to change notification settings - Fork 5
/
lib.nix
102 lines (98 loc) · 3.35 KB
/
lib.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
{ lib, pkgs, ... }:
lib.extend (
_: libprev: {
# namespace for custom functions
custom = rec {
# saner api for iterating through workspaces in a flat list
# takes a function that accepts the following attrset {workspace, key, monitor}
mapWorkspaces =
workspaceFn:
libprev.concatMap (
monitor:
libprev.forEach monitor.workspaces (
ws:
let
workspaceArg = {
inherit monitor;
workspace = toString ws;
key = toString (libprev.mod ws 10);
};
in
workspaceFn workspaceArg
)
);
# writeShellApplication with support for completions
writeShellApplicationCompletions =
{
name,
bashCompletion ? null,
zshCompletion ? null,
fishCompletion ? null,
...
}@shellArgs:
let
inherit (pkgs) writeShellApplication writeTextFile symlinkJoin;
# get the needed arguments for writeShellApplication
app = writeShellApplication (lib.intersectAttrs (lib.functionArgs writeShellApplication) shellArgs);
completions =
lib.optional (bashCompletion != null) (writeTextFile {
name = "${name}.bash";
destination = "/share/bash-completion/completions/${name}.bash";
text = bashCompletion;
})
++ lib.optional (zshCompletion != null) (writeTextFile {
name = "${name}.zsh";
destination = "/share/zsh/site-functions/_${name}";
text = zshCompletion;
})
++ lib.optional (fishCompletion != null) (writeTextFile {
name = "${name}.fish";
destination = "/share/fish/vendor_completions.d/${name}.fish";
text = fishCompletion;
});
in
if lib.length completions == 0 then
app
else
symlinkJoin {
inherit name;
inherit (app) meta;
paths = [ app ] ++ completions;
};
# produces an attrset shell package with completions from either a string / writeShellApplication attrset / package
mkShellPackages = lib.mapAttrs (
name: value:
if lib.isString value then
pkgs.writeShellApplication {
inherit name;
text = value;
}
# packages
else if lib.isDerivation value then
value
# attrs to pass to writeShellApplication
else
writeShellApplicationCompletions (value // { inherit name; })
);
# produces ini format strings, takes a single argument of the object
toQuotedINI = libprev.generators.toINI {
mkKeyValue = libprev.flip libprev.generators.mkKeyValueDefault "=" {
mkValueString =
v: if libprev.isString v then "\"${v}\"" else libprev.generators.mkValueStringDefault { } v;
};
};
# uses the direnv of a directory
direnvCargoRun =
{
dir,
bin ? builtins.baseNameOf dir,
args ? "",
}:
''
pushd ${dir} > /dev/null
${libprev.getExe pkgs.direnv} exec "${dir}" cargo run --release --bin "${bin}" --manifest-path "${dir}/Cargo.toml" -- ${args} "$@"
popd > /dev/null
'';
};
}
)