aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorEelco Dolstra <edolstra@gmail.com>2019-03-26 12:48:57 +0100
committerEelco Dolstra <edolstra@gmail.com>2019-03-26 12:48:57 +0100
commitf9c7176a87ccc71b719689cb28f8bc1bfb5354e3 (patch)
treeeeba937e0e0120aabcc34795e1aeea4e76c71a5a
parent42be60c6afbb5dbdbddd021df61b957061caa6bd (diff)
nix flake add: Handle ~/.config/nix not existing
Fixes $ nix flake add fnord github:edolstra/fnord error: opening file '/home/eelco/.config/nix/registry.json': No such file or directory
-rw-r--r--src/libexpr/primops/flake.cc21
1 files changed, 13 insertions, 8 deletions
diff --git a/src/libexpr/primops/flake.cc b/src/libexpr/primops/flake.cc
index b74e0b4b7..00eeba632 100644
--- a/src/libexpr/primops/flake.cc
+++ b/src/libexpr/primops/flake.cc
@@ -23,16 +23,20 @@ std::unique_ptr<FlakeRegistry> readRegistry(const Path & path)
{
auto registry = std::make_unique<FlakeRegistry>();
- auto json = nlohmann::json::parse(readFile(path));
+ try {
+ auto json = nlohmann::json::parse(readFile(path));
- auto version = json.value("version", 0);
- if (version != 1)
- throw Error("flake registry '%s' has unsupported version %d", path, version);
+ auto version = json.value("version", 0);
+ if (version != 1)
+ throw Error("flake registry '%s' has unsupported version %d", path, version);
- auto flakes = json["flakes"];
- for (auto i = flakes.begin(); i != flakes.end(); ++i) {
- FlakeRegistry::Entry entry{FlakeRef(i->value("uri", ""))};
- registry->entries.emplace(i.key(), entry);
+ auto flakes = json["flakes"];
+ for (auto i = flakes.begin(); i != flakes.end(); ++i) {
+ FlakeRegistry::Entry entry{FlakeRef(i->value("uri", ""))};
+ registry->entries.emplace(i.key(), entry);
+ }
+ } catch (SysError & e) {
+ if (e.errNo != ENOENT) throw;
}
return registry;
@@ -47,6 +51,7 @@ void writeRegistry(FlakeRegistry registry, Path path)
for (auto elem : registry.entries) {
json["flakes"][elem.first] = { {"uri", elem.second.ref.to_string()} };
}
+ createDirs(dirOf(path));
writeFile(path, json.dump(4)); // The '4' is the number of spaces used in the indentation in the json file.
}