Skip to content

Commit

Permalink
Merge pull request #11910 from DeterminateSystems/flake-init
Browse files Browse the repository at this point in the history
nix flake init: Operate on a SourcePath
  • Loading branch information
roberth authored Nov 19, 2024
2 parents 370dfcb + 868b4d3 commit 8637f3d
Showing 1 changed file with 20 additions and 25 deletions.
45 changes: 20 additions & 25 deletions src/nix/flake.cc
Original file line number Diff line number Diff line change
Expand Up @@ -891,37 +891,32 @@ struct CmdFlakeInitCommon : virtual Args, EvalCommand

auto cursor = installable.getCursor(*evalState);

auto templateDirAttr = cursor->getAttr("path");
auto templateDir = templateDirAttr->getString();

if (!store->isInStore(templateDir))
evalState->error<TypeError>(
"'%s' was not found in the Nix store\n"
"If you've set '%s' to a string, try using a path instead.",
templateDir, templateDirAttr->getAttrPathStr()).debugThrow();
auto templateDirAttr = cursor->getAttr("path")->forceValue();
NixStringContext context;
auto templateDir = evalState->coerceToPath(noPos, templateDirAttr, context, "");

std::vector<fs::path> changedFiles;
std::vector<fs::path> conflictedFiles;

std::function<void(const fs::path & from, const fs::path & to)> copyDir;
copyDir = [&](const fs::path & from, const fs::path & to)
std::function<void(const SourcePath & from, const fs::path & to)> copyDir;
copyDir = [&](const SourcePath & from, const fs::path & to)
{
fs::create_directories(to);

for (auto & entry : fs::directory_iterator{from}) {
for (auto & [name, entry] : from.readDirectory()) {
checkInterrupt();
auto from2 = entry.path();
auto to2 = to / entry.path().filename();
auto st = entry.symlink_status();
auto from2 = from / name;
auto to2 = to / name;
auto st = from2.lstat();
auto to_st = fs::symlink_status(to2);
if (fs::is_directory(st))
if (st.type == SourceAccessor::tDirectory)
copyDir(from2, to2);
else if (fs::is_regular_file(st)) {
auto contents = readFile(from2.string());
else if (st.type == SourceAccessor::tRegular) {
auto contents = from2.readFile();
if (fs::exists(to_st)) {
auto contents2 = readFile(to2.string());
if (contents != contents2) {
printError("refusing to overwrite existing file '%s'\n please merge it manually with '%s'", to2.string(), from2.string());
printError("refusing to overwrite existing file '%s'\n please merge it manually with '%s'", to2.string(), from2);
conflictedFiles.push_back(to2);
} else {
notice("skipping identical file: %s", from2);
Expand All @@ -930,18 +925,18 @@ struct CmdFlakeInitCommon : virtual Args, EvalCommand
} else
writeFile(to2, contents);
}
else if (fs::is_symlink(st)) {
auto target = fs::read_symlink(from2);
else if (st.type == SourceAccessor::tSymlink) {
auto target = from2.readLink();
if (fs::exists(to_st)) {
if (fs::read_symlink(to2) != target) {
printError("refusing to overwrite existing file '%s'\n please merge it manually with '%s'", to2.string(), from2.string());
printError("refusing to overwrite existing file '%s'\n please merge it manually with '%s'", to2.string(), from2);
conflictedFiles.push_back(to2);
} else {
notice("skipping identical file: %s", from2);
}
continue;
} else
fs::create_symlink(target, to2);
fs::create_symlink(target, to2);
}
else
throw Error("file '%s' has unsupported type", from2);
Expand All @@ -957,14 +952,14 @@ struct CmdFlakeInitCommon : virtual Args, EvalCommand
for (auto & s : changedFiles) args.emplace_back(s.string());
runProgram("git", true, args);
}
auto welcomeText = cursor->maybeGetAttr("welcomeText");
if (welcomeText) {

if (auto welcomeText = cursor->maybeGetAttr("welcomeText")) {
notice("\n");
notice(renderMarkdownToTerminal(welcomeText->getString()));
}

if (!conflictedFiles.empty())
throw Error("Encountered %d conflicts - see above", conflictedFiles.size());
throw Error("encountered %d conflicts - see above", conflictedFiles.size());
}
};

Expand Down

0 comments on commit 8637f3d

Please sign in to comment.