Skip to content

Commit

Permalink
WIP get settings info out of headers, out of libutil
Browse files Browse the repository at this point in the history
  • Loading branch information
Ericson2314 committed Jul 3, 2024
1 parent 509be0e commit 58f4d9e
Show file tree
Hide file tree
Showing 25 changed files with 184 additions and 65 deletions.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
40 changes: 40 additions & 0 deletions src/libmain/config-upstream.cc
Original file line number Diff line number Diff line change
@@ -0,0 +1,40 @@
namespace nix {

#include "fs-sink.hh"
#include "logging.hh"

struct LoggerSettings : Config
{
Setting<bool> showTrace{
this, false, "show-trace",
R"(
Whether Nix should print out a stack trace in case of Nix
expression evaluation errors.
)"};
};

static GlobalConfig::Register r1(&restoreSinkSettings);

struct RestoreSinkSettings : Config
{
Setting<bool> preallocateContents{this, false, "preallocate-contents",
"Whether to preallocate files when writing objects with known size."};
};

static GlobalConfig::Register rLoggerSettings(&loggerSettings);

struct ArchiveSettings : Config
{
Setting<bool> useCaseHack{this,
#if __APPLE__
true,
#else
false,
#endif
"use-case-hack",
"Whether to enable a Darwin-specific hack for dealing with file name collisions."};
};

static GlobalConfig::Register rArchiveSettings(&archiveSettings);

}
22 changes: 0 additions & 22 deletions src/libutil/config.cc → src/libmain/config.cc
Original file line number Diff line number Diff line change
Expand Up @@ -443,26 +443,4 @@ void OptionalPathSetting::operator =(const std::optional<Path> & v)
this->assign(v);
}

bool ExperimentalFeatureSettings::isEnabled(const ExperimentalFeature & feature) const
{
auto & f = experimentalFeatures.get();
return std::find(f.begin(), f.end(), feature) != f.end();
}

void ExperimentalFeatureSettings::require(const ExperimentalFeature & feature) const
{
if (!isEnabled(feature))
throw MissingExperimentalFeature(feature);
}

bool ExperimentalFeatureSettings::isEnabled(const std::optional<ExperimentalFeature> & feature) const
{
return !feature || isEnabled(*feature);
}

void ExperimentalFeatureSettings::require(const std::optional<ExperimentalFeature> & feature) const
{
if (feature) require(*feature);
}

}
3 changes: 0 additions & 3 deletions src/libutil/config.hh → src/libmain/config.hh
Original file line number Diff line number Diff line change
Expand Up @@ -257,9 +257,6 @@ public:
, documentDefault(documentDefault)
{ }

operator const T &() const { return value; }
operator T &() { return value; }
const T & get() const { return value; }
template<typename U>
bool operator ==(const U & v2) const { return value == v2; }
template<typename U>
Expand Down
1 change: 0 additions & 1 deletion src/libstore/derived-path.hh
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,6 @@
#include "path.hh"
#include "outputs-spec.hh"
#include "comparator.hh"
#include "config.hh"

#include <variant>

Expand Down
23 changes: 10 additions & 13 deletions src/libutil/archive.cc
Original file line number Diff line number Diff line change
Expand Up @@ -6,29 +6,26 @@
#include <strings.h> // for strcasecmp

#include "archive.hh"
#include "config-global.hh"
#include "posix-source-accessor.hh"
#include "source-path.hh"
#include "file-system.hh"
#include "signals.hh"

namespace nix {

struct ArchiveSettings : Config
{
Setting<bool> useCaseHack{this,
#if __APPLE__
true,
#else
false,
#endif
"use-case-hack",
"Whether to enable a Darwin-specific hack for dealing with file name collisions."};
const extern ArchiveSettings<JustValue> archiveSettingsDefaults = {
.useCaseHack = {
.value =
#if __APPLE__
true
#else
false
#endif
},
};

static ArchiveSettings archiveSettings;
ArchiveSettings<JustValue> archiveSettings = archiveSettingsDefaults;

static GlobalConfig::Register rArchiveSettings(&archiveSettings);

PathFilter defaultPathFilter = [](const Path &) { return true; };

Expand Down
11 changes: 11 additions & 0 deletions src/libutil/archive.hh
Original file line number Diff line number Diff line change
Expand Up @@ -2,12 +2,23 @@
///@file

#include "types.hh"
#include "config-abstract.hh"
#include "serialise.hh"
#include "fs-sink.hh"


namespace nix {

template<template<typename> class R>
struct ArchiveSettings
{
R<bool> useCaseHack;
};

const extern ArchiveSettings<JustValue> archiveSettingsDefaults;

// FIXME: don't use a global variable.
extern ArchiveSettings<JustValue> archiveSettings;

/**
* dumpPath creates a Nix archive of the specified path.
Expand Down
15 changes: 15 additions & 0 deletions src/libutil/config-abstract.hh
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
#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; }
};

}
37 changes: 37 additions & 0 deletions src/libutil/experimental-feature-settings.cc
Original file line number Diff line number Diff line change
@@ -0,0 +1,37 @@
#include "experimental-feature-settings.hh"

namespace nix {

const extern ExperimentalFeatureSettings experimentalFeatureSettingsDefaults = {
{
.experimentalFeatures = {
.value = {}
},
}
};

ExperimentalFeatureSettings experimentalFeatureSettings = experimentalFeatureSettingsDefaults;

bool ExperimentalFeatureSettings::isEnabled(const ExperimentalFeature & feature) const
{
auto & f = experimentalFeatures.get();
return std::find(f.begin(), f.end(), feature) != f.end();
}

void ExperimentalFeatureSettings::require(const ExperimentalFeature & feature) const
{
if (!isEnabled(feature))
throw MissingExperimentalFeature(feature);
}

bool ExperimentalFeatureSettings::isEnabled(const std::optional<ExperimentalFeature> & feature) const
{
return !feature || isEnabled(*feature);
}

void ExperimentalFeatureSettings::require(const std::optional<ExperimentalFeature> & feature) const
{
if (feature) require(*feature);
}

};
45 changes: 45 additions & 0 deletions src/libutil/experimental-feature-settings.hh
Original file line number Diff line number Diff line change
@@ -0,0 +1,45 @@
#pragma once
///@file

#include "experimental-features.hh"

namespace nix {

template<template<typename> class R>
struct ExperimentalFeatureSettingsT
{
R<std::set<ExperimentalFeature>> experimentalFeatures;
};

struct ExperimentalFeatureSettings : ExperimentalFeatureSettingsT<JustValue>
{
/**
* Check whether the given experimental feature is enabled.
*/
bool isEnabled(const ExperimentalFeature &) const;

/**
* Require an experimental feature be enabled, throwing an error if it is
* not.
*/
void require(const ExperimentalFeature &) const;

/**
* `std::nullopt` pointer means no feature, which means there is nothing that could be
* disabled, and so the function returns true in that case.
*/
bool isEnabled(const std::optional<ExperimentalFeature> &) const;

/**
* `std::nullopt` pointer means no feature, which means there is nothing that could be
* disabled, and so the function does nothing in that case.
*/
void require(const std::optional<ExperimentalFeature> &) const;
};

const extern ExperimentalFeatureSettings experimentalFeatureSettingsDefaults;

// FIXME: don't use a global variable.
extern ExperimentalFeatureSettings experimentalFeatureSettings;

}
1 change: 1 addition & 0 deletions src/libutil/experimental-features.hh
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@
///@file

#include "comparator.hh"
#include "config-abstract.hh"
#include "error.hh"
#include "json-utils.hh"
#include "types.hh"
Expand Down
11 changes: 1 addition & 10 deletions src/libutil/fs-sink.cc
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
#include <fcntl.h>

#include "error.hh"
#include "config-global.hh"
#include "fs-sink.hh"

#if _WIN32
Expand Down Expand Up @@ -58,15 +57,7 @@ void copyRecursive(
}


struct RestoreSinkSettings : Config
{
Setting<bool> preallocateContents{this, false, "preallocate-contents",
"Whether to preallocate files when writing objects with known size."};
};

static RestoreSinkSettings restoreSinkSettings;

static GlobalConfig::Register r1(&restoreSinkSettings);
RestoreSinkSettings<JustValue> restoreSinkSettings;


void RestoreSink::createDirectory(const CanonPath & path)
Expand Down
10 changes: 10 additions & 0 deletions src/libutil/fs-sink.hh
Original file line number Diff line number Diff line change
Expand Up @@ -2,12 +2,20 @@
///@file

#include "types.hh"
#include "config-abstract.hh"
#include "serialise.hh"
#include "source-accessor.hh"
#include "file-system.hh"

namespace nix {

template<template<typename> class R>
struct RestoreSinkSettings
{
R<bool> preallocateContents;
};


/**
* Actions on an open regular file in the process of creating it.
*
Expand All @@ -23,6 +31,8 @@ struct CreateRegularFileSink : Sink
virtual void preallocateContents(uint64_t size) { };
};

// FIXME: don't use a global variable.
extern RestoreSinkSettings<JustValue> restoreSinkSettings;

struct FileSystemObjectSink
{
Expand Down
2 changes: 1 addition & 1 deletion src/libutil/git.cc
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@
#include <strings.h> // for strcasecmp

#include "signals.hh"
#include "config.hh"
#include "config-abstract.hh"
#include "hash.hh"

#include "git.hh"
Expand Down
1 change: 1 addition & 0 deletions src/libutil/git.hh
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@
#include <optional>

#include "types.hh"
#include "experimental-feature-settings.hh"
#include "serialise.hh"
#include "hash.hh"
#include "source-path.hh"
Expand Down
1 change: 0 additions & 1 deletion src/libutil/hash.cc
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,6 @@
#include <openssl/md5.h>
#include <openssl/sha.h>

#include "args.hh"
#include "hash.hh"
#include "archive.hh"
#include "split.hh"
Expand Down
9 changes: 6 additions & 3 deletions src/libutil/logging.cc
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,6 @@
#include "environment-variables.hh"
#include "terminal.hh"
#include "util.hh"
#include "config-global.hh"
#include "source-path.hh"
#include "position.hh"

Expand All @@ -14,9 +13,13 @@

namespace nix {

LoggerSettings loggerSettings;
const extern LoggerSettings<JustValue> loggerSettingsDefaults = {
.showTrace = {
.value = false
},
};

static GlobalConfig::Register rLoggerSettings(&loggerSettings);
LoggerSettings<JustValue> loggerSettings = loggerSettingsDefaults;

static thread_local ActivityId curActivity = 0;

Expand Down
14 changes: 5 additions & 9 deletions src/libutil/logging.hh
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@

#include "types.hh"
#include "error.hh"
#include "config.hh"
#include "config-abstract.hh"

#include <nlohmann/json_fwd.hpp>

Expand Down Expand Up @@ -40,17 +40,13 @@ typedef enum {

typedef uint64_t ActivityId;

struct LoggerSettings : Config
template<template<typename> class R>
struct LoggerSettings
{
Setting<bool> showTrace{
this, false, "show-trace",
R"(
Whether Nix should print out a stack trace in case of Nix
expression evaluation errors.
)"};
R<bool> showTrace;
};

extern LoggerSettings loggerSettings;
extern LoggerSettings<JustValue> loggerSettings;

class Logger
{
Expand Down
Loading

0 comments on commit 58f4d9e

Please sign in to comment.