From c300efc0e1ff021c2afa68ea1e470f55d31e3718 Mon Sep 17 00:00:00 2001 From: Rebecca Turner Date: Fri, 23 Aug 2024 16:59:38 -0700 Subject: [PATCH] Add `ApplyConfigOptions` Change-Id: Ic876bcabd0b68e579bbd30ca1755919df43d4813 --- src/libutil/apply-config-options.hh | 53 +++++++++++++++++++++++++++++ src/libutil/meson.build | 1 + 2 files changed, 54 insertions(+) create mode 100644 src/libutil/apply-config-options.hh diff --git a/src/libutil/apply-config-options.hh b/src/libutil/apply-config-options.hh new file mode 100644 index 000000000..533904a69 --- /dev/null +++ b/src/libutil/apply-config-options.hh @@ -0,0 +1,53 @@ +#pragma once +/** + * @file + * @brief Options for applying `Config` settings. + */ + +#include + +namespace nix { + +/** + * Options for applying `Config` settings. + */ +struct ApplyConfigOptions +{ + /** + * The configuration file being loaded. + * + * If set, relative paths are allowed and interpreted as relative to the + * directory of this path. + */ + std::optional path = std::nullopt; + + /** + * If set, tilde paths (like `~/.config/repl.nix`) are allowed and the + * tilde is substituted for this directory. + */ + std::optional home = std::nullopt; + + /** + * Is the configuration being loaded from the `$NIX_CONFIG` environment + * variable? + * + * Used for formatting error messages. + */ + bool fromEnvVar = false; + + /** + * Display the `relative` path field, with a reasonable default if none is + * available. + */ + std::string relativeDisplay() const { + if (path) { + return *path; + } else if (fromEnvVar) { + return "$NIX_CONFIG"; + } else { + return ""; + } + } +}; + +} diff --git a/src/libutil/meson.build b/src/libutil/meson.build index 3e10e4b63..7c0e45e4b 100644 --- a/src/libutil/meson.build +++ b/src/libutil/meson.build @@ -46,6 +46,7 @@ libutil_sources = files( ) libutil_headers = files( + 'apply-config-options.hh', 'abstract-setting-to-json.hh', 'ansicolor.hh', 'archive.hh',