Skip to content

Commit

Permalink
WIP Start getting store settings off config.hh
Browse files Browse the repository at this point in the history
  • Loading branch information
Ericson2314 committed Jul 17, 2024
1 parent fcf3e32 commit 3b0edea
Show file tree
Hide file tree
Showing 6 changed files with 103 additions and 16 deletions.
14 changes: 14 additions & 0 deletions src/libstore/config-parse.hh
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
#pragma once

/**
* Look up the setting's name in a map, falling back on the default if
* it does not exist.
*/
#define CONFIG_ROW(FIELD) \
.FIELD = { \
.value = ({ \
auto p = get(params, descriptions.FIELD.name); \
p ? *p : defaults.FIELD.value; \
}) \
}

2 changes: 1 addition & 1 deletion src/libstore/path.cc
Original file line number Diff line number Diff line change
Expand Up @@ -89,7 +89,7 @@ StorePath StoreDirConfig::parseStorePath(std::string_view path) const
canonPath(std::string(path))
#endif
;
if (dirOf(p) != storeDir)
if (dirOf(p) != storeDir.get())
throw BadStorePath("path '%s' is not in the Nix store", p);
return StorePath(baseNameOf(p));
}
Expand Down
2 changes: 0 additions & 2 deletions src/libstore/store-api.hh
Original file line number Diff line number Diff line change
Expand Up @@ -99,8 +99,6 @@ typedef std::map<StorePath, std::optional<ContentAddress>> StorePathCAMap;

struct StoreConfig : public StoreDirConfig
{
using Params = StoreReference::Params;

using StoreDirConfig::StoreDirConfig;

StoreConfig() = delete;
Expand Down
41 changes: 41 additions & 0 deletions src/libstore/store-dir-config.cc
Original file line number Diff line number Diff line change
@@ -0,0 +1,41 @@
#include "store-dir-config.hh"
#include "config-parse.hh"
#include "util.hh"

namespace nix {

const StoreDirConfigT<JustValue> storeDirConfigDefaults = {
.storeDir =
{
.value = settings.nixStore,
},
};

const StoreDirConfigT<SettingInfo> storeDirConfigDescriptions = {
.storeDir =
{
.name = "store",
.description = R"(
Logical location of the Nix store, usually
`/nix/store`. Note that you can only copy store paths
between stores if they have the same `store` setting.
)",
},
};

StoreDirConfigT<JustValue> parseStoreDirConfig(const StoreReference::Params & params)
{
constexpr auto & defaults = storeDirConfigDefaults;
constexpr auto & descriptions = storeDirConfigDescriptions;

return {
CONFIG_ROW(storeDir),
};
}

StoreDirConfig::StoreDirConfig(const StoreReference::Params & params)
: StoreDirConfigT<JustValue>{parseStoreDirConfig(params)}
{
}

}
28 changes: 15 additions & 13 deletions src/libstore/store-dir-config.hh
Original file line number Diff line number Diff line change
Expand Up @@ -3,8 +3,9 @@
#include "path.hh"
#include "hash.hh"
#include "content-address.hh"
#include "store-reference.hh"
#include "config-abstract.hh"
#include "globals.hh"
#include "config.hh"

#include <map>
#include <string>
Expand All @@ -18,22 +19,23 @@ struct SourcePath;
MakeError(BadStorePath, Error);
MakeError(BadStorePathName, BadStorePath);

struct StoreDirConfig : public Config
template<template<typename> class F>
struct StoreDirConfigT
{
using Config::Config;
F<Path> storeDir;
};

StoreDirConfig() = delete;
extern const StoreDirConfigT<JustValue> storeDirConfigDefaults;

virtual ~StoreDirConfig() = default;
extern const StoreDirConfigT<SettingInfo> storeDirConfigDescriptions;

StoreDirConfigT<JustValue> parseStoreDirConfig(const StoreReference::Params &);

const PathSetting storeDir_{this, settings.nixStore,
"store",
R"(
Logical location of the Nix store, usually
`/nix/store`. Note that you can only copy store paths
between stores if they have the same `store` setting.
)"};
const Path storeDir = storeDir_;
struct StoreDirConfig : StoreDirConfigT<JustValue>
{
StoreDirConfig(const StoreReference::Params & params);

virtual ~StoreDirConfig() = default;

// pure methods

Expand Down
32 changes: 32 additions & 0 deletions src/libutil/config-abstract.hh
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
#pragma once
///@type

namespace nix {

template<typename T>
struct JustValue
{
T value;

operator const T &() const
{
return value;
}
operator T &()
{
return value;
}
const T & get() const
{
return value;
}
};

template<typename T>
struct SettingInfo
{
std::string name;
std::string description;
};

}

0 comments on commit 3b0edea

Please sign in to comment.