-
Notifications
You must be signed in to change notification settings - Fork 0
/
flake.nix
129 lines (120 loc) · 3.88 KB
/
flake.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
{
inputs.nixpkgs.url = "github:nixos/nixpkgs/nixos-23.05?dir=lib";
outputs = { self, nixpkgs }:
let
lib = nixpkgs.lib;
in
{
lib.packagesFromVersionsFile =
{ versionsFile ? null
, legacyVersionFiles ? { }
, system ? builtins.currentSystem
, plugins ? { }
, skipMissingPlugins ? false
}:
let
fileLines = file: lib.splitString "\n" (lib.fileContents file);
removeComments = builtins.filter (line: !lib.hasPrefix "#" line);
parseVersions = builtins.map
(line:
let
pluginAndVersion = lib.splitString " " line;
in
{
name = builtins.elemAt pluginAndVersion 0;
value = builtins.elemAt pluginAndVersion 1;
});
toolVersions =
if versionsFile == null
then { }
else
builtins.listToAttrs
(parseVersions
(removeComments
(fileLines versionsFile)));
legacyVersions =
builtins.mapAttrs
(_: file: lib.fileContents file)
legacyVersionFiles;
versions =
lib.throwIf (versionsFile == null && legacyVersionFiles == { })
''
No version files provided. Try with `versionsFile`:
```
packagesFromVersionsFile {
versionsFile = ./.tool-versions;
...
}
```
Or `legacyVersionFiles`:
```
packagesFromVersionsFile {
legacyVersionFiles = {
python = ./.python-version;
};
...
}
```
''
toolVersions // legacyVersions;
filterPlugins = lib.filterAttrs
(name: _:
let
hasPlugin = builtins.hasAttr name plugins;
in
lib.throwIf (!skipMissingPlugins && !hasPlugin)
''
No plugin found for "${name}", try adding the missing plugin:
```
<asdf2nix>.lib.packagesFromVersionsFile {
plugins = {
${name} = <asdf2nix-${name}>.lib.packageFromVersion;
...
};
...
};
```
Or enable `skipMissingPlugins` to skip this error:
```
<asdf2nix>.lib.packagesFromVersionsFile {
plugins = { ... };
skipMissingPlugins = true;
...
};
```
''
lib.warnIf
(!hasPlugin) "Skipping \"${name}\" plugin"
hasPlugin);
findPackages = builtins.mapAttrs
(name: version:
let
plugin = plugins.${name};
in
lib.throwIf (!plugin.hasVersion { inherit system version; })
''
Plugin "${name}" does not provide version "${version}", try
updating the plugin's input:
```
> nix flake lock --update-input <asdf2nix-${name}>
```
''
plugin.packageFromVersion
{
inherit system version;
}
);
in
findPackages (filterPlugins versions);
templates = {
default = {
description = "Install Nix packages from asdf versions file";
path = ./templates/default;
};
devenv = {
description = "Integration with devenv";
path = ./templates/devenv;
};
};
};
}