Nix 2.36.0
Nix, the purely functional package manager; unstable internal interfaces
Loading...
Searching...
No Matches
store-registration.hh
Go to the documentation of this file.
1#pragma once
15
17#include "nix/util/url.hh"
18
19namespace nix {
20
22{
26 std::string doc;
27
32
37 std::optional<ExperimentalFeature> experimentalFeature;
38
43 fun<ref<StoreConfig>(std::string_view scheme, std::string_view authorityPath, const Store::Config::Params & params)>
45
52};
53
55{
56 using Map = std::map<std::string, StoreFactory>;
57
58 static Map & registered();
59
60 template<typename TConfig>
61 static void add()
62 {
63 StoreFactory factory{
64 .doc = TConfig::doc(),
65 .uriSchemes = TConfig::uriSchemes(),
66 .experimentalFeature = TConfig::experimentalFeature(),
67 .parseConfig = ([](auto scheme, auto uri, auto & params) -> ref<StoreConfig> {
68 if constexpr (std::is_constructible_v<TConfig, std::filesystem::path, StoreConfig::Params>) {
69 auto path =
70 uri.empty()
71 ? std::filesystem::path{}
72 : canonPath(urlPathToPath(splitString<std::vector<std::string>>(percentDecode(uri), "/")));
73 return make_ref<TConfig>(std::move(path), params);
74 } else if constexpr (std::is_constructible_v<TConfig, ParsedURL, StoreConfig::Params>) {
75 return make_ref<TConfig>(parseURL(concatStrings(scheme, "://", uri)), params);
76 } else if constexpr (std::is_constructible_v<TConfig, ParsedURL::Authority, StoreConfig::Params>) {
77 return make_ref<TConfig>(ParsedURL::Authority::parse(uri), params);
78 } else {
79 return make_ref<TConfig>(scheme, uri, params);
80 }
81 }),
82 .getConfig = ([]() -> ref<StoreConfig> { return make_ref<TConfig>(Store::Config::Params{}); }),
83 };
84 auto [it, didInsert] = registered().insert({TConfig::name(), std::move(factory)});
85 if (!didInsert) {
86 throw Error("Already registered store with name '%s'", it->first);
87 }
88 }
89};
90
91template<typename TConfig>
92struct RegisterStoreImplementation
93{
94 RegisterStoreImplementation()
95 {
96 Implementations::add<TConfig>();
97 }
98};
99
100} // namespace nix
Definition error.hh:310
Definition fun.hh:19
Definition ref.hh:58
Definition store-registration.hh:55
Definition store-registration.hh:22
std::optional< ExperimentalFeature > experimentalFeature
Definition store-registration.hh:37
fun< ref< StoreConfig >()> getConfig
Definition store-registration.hh:51
StringSet uriSchemes
Definition store-registration.hh:31
fun< ref< StoreConfig >(std::string_view scheme, std::string_view authorityPath, const Store::Config::Params &params)> parseConfig
Definition store-registration.hh:44
std::string doc
Definition store-registration.hh:26
std::set< std::string, std::less<> > StringSet
Definition types.hh:45
ParsedURL parseURL(std::string_view url, bool lenient=false)
Definition url.cc:120
std::filesystem::path urlPathToPath(std::span< const std::string > urlPath)
Definition url.cc:746