1
1
Fork 0
mirror of https://github.com/NixOS/nix.git synced 2025-11-19 00:39:37 +01:00

Apply clang-format universally.

* It is tough to contribute to a project that doesn't use a formatter,
* It is extra hard to contribute to a project which has configured the formatter, but ignores it for some files
* Code formatting makes it harder to hide obscure / weird bugs by accident or on purpose,

Let's rip the bandaid off?

Note that PRs currently in flight should be able to be merged relatively easily by applying `clang-format` to their tip prior to merge.
This commit is contained in:
Graham Christensen 2025-07-18 12:47:27 -04:00
parent 41bf87ec70
commit e4f62e4608
587 changed files with 23258 additions and 23135 deletions

View file

@ -33,7 +33,7 @@
using namespace nix;
using namespace std::string_literals;
extern char * * environ __attribute__((weak));
extern char ** environ __attribute__((weak));
/* Recreate the effect of the perl shellwords function, breaking up a
* string into arguments like a shell word, including escapes
@ -44,11 +44,9 @@ static std::vector<std::string> shellwords(std::string_view s)
auto begin = s.cbegin();
std::vector<std::string> res;
std::string cur;
enum state {
sBegin,
sSingleQuote,
sDoubleQuote
};
enum state { sBegin, sSingleQuote, sDoubleQuote };
state st = sBegin;
auto it = begin;
for (; it != s.cend(); ++it) {
@ -58,36 +56,38 @@ static std::vector<std::string> shellwords(std::string_view s)
cur.append(begin, it);
res.push_back(cur);
it = match[0].second;
if (it == s.cend()) return res;
if (it == s.cend())
return res;
begin = it;
cur.clear();
}
}
switch (*it) {
case '\'':
if (st != sDoubleQuote) {
cur.append(begin, it);
begin = it + 1;
st = st == sBegin ? sSingleQuote : sBegin;
}
break;
case '"':
if (st != sSingleQuote) {
cur.append(begin, it);
begin = it + 1;
st = st == sBegin ? sDoubleQuote : sBegin;
}
break;
case '\\':
if (st != sSingleQuote) {
/* perl shellwords mostly just treats the next char as part of the string with no special processing */
cur.append(begin, it);
begin = ++it;
}
break;
case '\'':
if (st != sDoubleQuote) {
cur.append(begin, it);
begin = it + 1;
st = st == sBegin ? sSingleQuote : sBegin;
}
break;
case '"':
if (st != sSingleQuote) {
cur.append(begin, it);
begin = it + 1;
st = st == sBegin ? sDoubleQuote : sBegin;
}
break;
case '\\':
if (st != sSingleQuote) {
/* perl shellwords mostly just treats the next char as part of the string with no special processing */
cur.append(begin, it);
begin = ++it;
}
break;
}
}
if (st != sBegin) throw Error("unterminated quote in shebang line");
if (st != sBegin)
throw Error("unterminated quote in shebang line");
cur.append(begin, it);
res.push_back(cur);
return res;
@ -106,7 +106,8 @@ static SourcePath resolveShellExprPath(SourcePath path)
if (compatibilitySettings.nixShellAlwaysLooksForShellNix) {
return resolvedOrDir / "shell.nix";
} else {
warn("Skipping '%1%', because the setting '%2%' is disabled. This is a deprecated behavior. Consider enabling '%2%'.",
warn(
"Skipping '%1%', because the setting '%2%' is disabled. This is a deprecated behavior. Consider enabling '%2%'.",
resolvedOrDir / "shell.nix",
"nix-shell-always-looks-for-shell-nix");
}
@ -119,7 +120,7 @@ static SourcePath resolveShellExprPath(SourcePath path)
return resolvedOrDir;
}
static void main_nix_build(int argc, char * * argv)
static void main_nix_build(int argc, char ** argv)
{
auto dryRun = false;
auto isNixShell = std::regex_search(argv[0], std::regex("nix-shell$"));
@ -148,9 +149,21 @@ static void main_nix_build(int argc, char * * argv)
// List of environment variables kept for --pure
StringSet keepVars{
"HOME", "XDG_RUNTIME_DIR", "USER", "LOGNAME", "DISPLAY",
"WAYLAND_DISPLAY", "WAYLAND_SOCKET", "PATH", "TERM", "IN_NIX_SHELL",
"NIX_SHELL_PRESERVE_PROMPT", "TZ", "PAGER", "NIX_BUILD_SHELL", "SHLVL",
"HOME",
"XDG_RUNTIME_DIR",
"USER",
"LOGNAME",
"DISPLAY",
"WAYLAND_DISPLAY",
"WAYLAND_SOCKET",
"PATH",
"TERM",
"IN_NIX_SHELL",
"NIX_SHELL_PRESERVE_PROMPT",
"TZ",
"PAGER",
"NIX_BUILD_SHELL",
"SHLVL",
};
keepVars.insert(networkProxyVariables.begin(), networkProxyVariables.end());
@ -179,13 +192,16 @@ static void main_nix_build(int argc, char * * argv)
args.push_back(word);
}
}
} catch (SystemError &) { }
} catch (SystemError &) {
}
}
struct MyArgs : LegacyArgs, MixEvalArgs
{
using LegacyArgs::LegacyArgs;
void setBaseDir(Path baseDir) {
void setBaseDir(Path baseDir)
{
commandBaseDir = baseDir;
}
};
@ -235,8 +251,10 @@ static void main_nix_build(int argc, char * * argv)
else if (*arg == "--expr" || *arg == "-E")
fromArgs = true;
else if (*arg == "--pure") pure = true;
else if (*arg == "--impure") pure = false;
else if (*arg == "--pure")
pure = true;
else if (*arg == "--impure")
pure = false;
else if (isNixShell && (*arg == "--packages" || *arg == "-p"))
packages = true;
@ -262,9 +280,15 @@ static void main_nix_build(int argc, char * * argv)
// read the shebang to understand which packages to read from. Since
// this is handled via nix-shell -p, we wrap our ruby script execution
// in ruby -e 'load' which ignores the shebangs.
envCommand = fmt("exec %1% %2% -e 'load(ARGV.shift)' -- %3% %4%", execArgs, interpreter, escapeShellArgAlways(script), toView(joined));
envCommand =
fmt("exec %1% %2% -e 'load(ARGV.shift)' -- %3% %4%",
execArgs,
interpreter,
escapeShellArgAlways(script),
toView(joined));
} else {
envCommand = fmt("exec %1% %2% %3% %4%", execArgs, interpreter, escapeShellArgAlways(script), toView(joined));
envCommand =
fmt("exec %1% %2% %3% %4%", execArgs, interpreter, escapeShellArgAlways(script), toView(joined));
}
}
@ -293,7 +317,8 @@ static void main_nix_build(int argc, char * * argv)
auto state = std::make_unique<EvalState>(myArgs.lookupPath, evalStore, fetchSettings, evalSettings, store);
state->repair = myArgs.repair;
if (myArgs.repair) buildMode = bmRepair;
if (myArgs.repair)
buildMode = bmRepair;
if (inShebang && compatibilitySettings.nixShellShebangArgumentsRelativeToScript) {
myArgs.setBaseDir(absPath(dirOf(script)));
@ -304,20 +329,23 @@ static void main_nix_build(int argc, char * * argv)
if (isNixShell) {
auto newArgs = state->buildBindings(autoArgsWithInNixShell->size() + 1);
newArgs.alloc("inNixShell").mkBool(true);
for (auto & i : *autoArgs) newArgs.insert(i);
for (auto & i : *autoArgs)
newArgs.insert(i);
autoArgsWithInNixShell = newArgs.finish();
}
if (packages) {
std::ostringstream joined;
joined << "{...}@args: with import <nixpkgs> args; (pkgs.runCommandCC or pkgs.runCommand) \"shell\" { buildInputs = [ ";
joined
<< "{...}@args: with import <nixpkgs> args; (pkgs.runCommandCC or pkgs.runCommand) \"shell\" { buildInputs = [ ";
for (const auto & i : remainingArgs)
joined << '(' << i << ") ";
joined << "]; } \"\"";
fromArgs = true;
remainingArgs = {joined.str()};
} else if (!fromArgs && remainingArgs.empty()) {
if (isNixShell && !compatibilitySettings.nixShellAlwaysLooksForShellNix && std::filesystem::exists("shell.nix")) {
if (isNixShell && !compatibilitySettings.nixShellAlwaysLooksForShellNix
&& std::filesystem::exists("shell.nix")) {
// If we're in 2.3 compatibility mode, we need to look for shell.nix
// now, because it won't be done later.
remainingArgs = {"shell.nix"};
@ -326,7 +354,10 @@ static void main_nix_build(int argc, char * * argv)
// Instead of letting it throw later, we throw here to give a more relevant error message
if (isNixShell && !std::filesystem::exists("shell.nix") && !std::filesystem::exists("default.nix"))
throw Error("no argument specified and no '%s' or '%s' file found in the working directory", "shell.nix", "default.nix");
throw Error(
"no argument specified and no '%s' or '%s' file found in the working directory",
"shell.nix",
"default.nix");
}
}
@ -348,14 +379,13 @@ static void main_nix_build(int argc, char * * argv)
std::move(i),
(inShebang && compatibilitySettings.nixShellShebangArgumentsRelativeToScript)
? lookupFileArg(*state, shebangBaseDir)
: state->rootPath(".")
));
}
else {
: state->rootPath(".")));
} else {
auto absolute = i;
try {
absolute = canonPath(absPath(i), true);
} catch (Error & e) {};
} catch (Error & e) {
};
auto [path, outputNames] = parsePathWithOutputs(absolute);
if (evalStore->isStorePath(path) && hasSuffix(path, ".drv"))
drvs.push_back(PackageInfo(*state, evalStore, absolute));
@ -364,10 +394,8 @@ static void main_nix_build(int argc, char * * argv)
relative to the script. */
auto baseDir = inShebang && !packages ? absPath(i, absPath(dirOf(script))) : i;
auto sourcePath = lookupFileArg(*state,
baseDir);
auto resolvedPath =
isNixShell ? resolveShellExprPath(sourcePath) : resolveExprPath(sourcePath);
auto sourcePath = lookupFileArg(*state, baseDir);
auto resolvedPath = isNixShell ? resolveShellExprPath(sourcePath) : resolveExprPath(sourcePath);
exprs.push_back(state->parseExprFromFile(resolvedPath));
}
@ -375,7 +403,8 @@ static void main_nix_build(int argc, char * * argv)
}
/* Evaluate them into derivations. */
if (attrPaths.empty()) attrPaths = {""};
if (attrPaths.empty())
attrPaths = {""};
for (auto e : exprs) {
Value vRoot;
@ -399,21 +428,11 @@ static void main_nix_build(int argc, char * * argv)
};
for (auto & i : attrPaths) {
Value & v(*findAlongAttrPath(
*state,
i,
takesNixShellAttr(vRoot) ? *autoArgsWithInNixShell : *autoArgs,
vRoot
).first);
Value & v(
*findAlongAttrPath(*state, i, takesNixShellAttr(vRoot) ? *autoArgsWithInNixShell : *autoArgs, vRoot)
.first);
state->forceValue(v, v.determinePos(noPos));
getDerivations(
*state,
v,
"",
takesNixShellAttr(v) ? *autoArgsWithInNixShell : *autoArgs,
drvs,
false
);
getDerivations(*state, v, "", takesNixShellAttr(v) ? *autoArgsWithInNixShell : *autoArgs, drvs, false);
}
}
@ -446,9 +465,7 @@ static void main_nix_build(int argc, char * * argv)
if (!shell) {
try {
auto expr = state->parseExprFromString(
"(import <nixpkgs> {}).bashInteractive",
state->rootPath("."));
auto expr = state->parseExprFromString("(import <nixpkgs> {}).bashInteractive", state->rootPath("."));
Value v;
state->eval(expr, v);
@ -458,10 +475,11 @@ static void main_nix_build(int argc, char * * argv)
throw Error("the 'bashInteractive' attribute in <nixpkgs> did not evaluate to a derivation");
auto bashDrv = drv->requireDrvPath();
pathsToBuild.push_back(DerivedPath::Built {
.drvPath = makeConstantStorePathRef(bashDrv),
.outputs = OutputsSpec::Names {"out"},
});
pathsToBuild.push_back(
DerivedPath::Built{
.drvPath = makeConstantStorePathRef(bashDrv),
.outputs = OutputsSpec::Names{"out"},
});
pathsToCopy.insert(bashDrv);
shellDrv = bashDrv;
@ -474,16 +492,17 @@ static void main_nix_build(int argc, char * * argv)
std::function<void(ref<SingleDerivedPath>, const DerivedPathMap<StringSet>::ChildNode &)> accumDerivedPath;
accumDerivedPath = [&](ref<SingleDerivedPath> inputDrv, const DerivedPathMap<StringSet>::ChildNode & inputNode) {
accumDerivedPath = [&](ref<SingleDerivedPath> inputDrv,
const DerivedPathMap<StringSet>::ChildNode & inputNode) {
if (!inputNode.value.empty())
pathsToBuild.push_back(DerivedPath::Built {
.drvPath = inputDrv,
.outputs = OutputsSpec::Names { inputNode.value },
});
pathsToBuild.push_back(
DerivedPath::Built{
.drvPath = inputDrv,
.outputs = OutputsSpec::Names{inputNode.value},
});
for (const auto & [outputName, childNode] : inputNode.childMap)
accumDerivedPath(
make_ref<SingleDerivedPath>(SingleDerivedPath::Built { inputDrv, outputName }),
childNode);
make_ref<SingleDerivedPath>(SingleDerivedPath::Built{inputDrv, outputName}), childNode);
};
// Build or fetch all dependencies of the derivation.
@ -491,11 +510,9 @@ static void main_nix_build(int argc, char * * argv)
// To get around lambda capturing restrictions in the
// standard.
const auto & inputDrv = inputDrv0;
if (std::all_of(envExclude.cbegin(), envExclude.cend(),
[&](const std::string & exclude) {
return !std::regex_search(store->printStorePath(inputDrv), std::regex(exclude));
}))
{
if (std::all_of(envExclude.cbegin(), envExclude.cend(), [&](const std::string & exclude) {
return !std::regex_search(store->printStorePath(inputDrv), std::regex(exclude));
})) {
accumDerivedPath(makeConstantStorePathRef(inputDrv), inputNode);
pathsToCopy.insert(inputDrv);
}
@ -507,7 +524,8 @@ static void main_nix_build(int argc, char * * argv)
buildPaths(pathsToBuild);
if (dryRun) return;
if (dryRun)
return;
if (shellDrv) {
auto shellDrvOutputs = store->queryPartialDerivationOutputMap(shellDrv.value(), &*evalStore);
@ -540,9 +558,7 @@ static void main_nix_build(int argc, char * * argv)
auto parsedDrv = StructuredAttrs::tryParse(drv.env);
DerivationOptions drvOptions;
try {
drvOptions = DerivationOptions::fromStructuredAttrs(
drv.env,
parsedDrv ? &*parsedDrv : nullptr);
drvOptions = DerivationOptions::fromStructuredAttrs(drv.env, parsedDrv ? &*parsedDrv : nullptr);
} catch (Error & e) {
e.addTrace({}, "while parsing derivation '%s'", store->printStorePath(packageInfo.requireDrvPath()));
throw;
@ -566,7 +582,8 @@ static void main_nix_build(int argc, char * * argv)
std::function<void(const StorePath &, const DerivedPathMap<StringSet>::ChildNode &)> accumInputClosure;
accumInputClosure = [&](const StorePath & inputDrv, const DerivedPathMap<StringSet>::ChildNode & inputNode) {
accumInputClosure = [&](const StorePath & inputDrv,
const DerivedPathMap<StringSet>::ChildNode & inputNode) {
auto outputs = store->queryPartialDerivationOutputMap(inputDrv, &*evalStore);
for (auto & i : inputNode.value) {
auto o = outputs.at(i);
@ -579,11 +596,7 @@ static void main_nix_build(int argc, char * * argv)
for (const auto & [inputDrv, inputNode] : drv.inputDrvs.map)
accumInputClosure(inputDrv, inputNode);
auto json = parsedDrv->prepareStructuredAttrs(
*store,
drvOptions,
inputs,
drv.outputs);
auto json = parsedDrv->prepareStructuredAttrs(*store, drvOptions, inputs, drv.outputs);
structuredAttrsRC = StructuredAttrs::writeShell(json);
@ -644,9 +657,7 @@ static void main_nix_build(int argc, char * * argv)
for (auto & i : env)
envStrs.push_back(i.first + "=" + i.second);
auto args = interactive
? Strings{"bash", "--rcfile", rcfile}
: Strings{"bash", rcfile};
auto args = interactive ? Strings{"bash", "--rcfile", rcfile} : Strings{"bash", rcfile};
auto envPtrs = stringsToCharPtrs(envStrs);
@ -678,10 +689,11 @@ static void main_nix_build(int argc, char * * argv)
if (outputName == "")
throw Error("derivation '%s' lacks an 'outputName' attribute", store->printStorePath(drvPath));
pathsToBuild.push_back(DerivedPath::Built{
.drvPath = makeConstantStorePathRef(drvPath),
.outputs = OutputsSpec::Names{outputName},
});
pathsToBuild.push_back(
DerivedPath::Built{
.drvPath = makeConstantStorePathRef(drvPath),
.outputs = OutputsSpec::Names{outputName},
});
pathsToBuildOrdered.push_back({drvPath, {outputName}});
drvsToCopy.insert(drvPath);
@ -694,7 +706,8 @@ static void main_nix_build(int argc, char * * argv)
buildPaths(pathsToBuild);
if (dryRun) return;
if (dryRun)
return;
std::vector<StorePath> outPaths;
@ -712,7 +725,8 @@ static void main_nix_build(int argc, char * * argv)
if (auto store2 = store.dynamic_pointer_cast<LocalFSStore>()) {
std::string symlink = drvPrefix;
if (outputName != "out") symlink += "-" + outputName;
if (outputName != "out")
symlink += "-" + outputName;
store2->addPermRoot(outputPath, absPath(symlink));
}