2004-02-06 16:57:10 +02:00
|
|
|
#include "profiles.hh"
|
2006-12-05 04:18:46 +02:00
|
|
|
#include "store-api.hh"
|
2020-10-09 23:18:08 +03:00
|
|
|
#include "local-fs-store.hh"
|
2023-10-25 07:43:36 +03:00
|
|
|
#include "users.hh"
|
2004-02-06 16:57:10 +02:00
|
|
|
|
2004-02-06 18:03:27 +02:00
|
|
|
#include <sys/types.h>
|
|
|
|
#include <sys/stat.h>
|
|
|
|
#include <unistd.h>
|
2004-07-01 14:11:16 +03:00
|
|
|
#include <errno.h>
|
2005-05-04 19:33:20 +03:00
|
|
|
#include <stdio.h>
|
2004-02-06 16:57:10 +02:00
|
|
|
|
2004-02-06 18:03:27 +02:00
|
|
|
|
2006-09-05 00:06:23 +03:00
|
|
|
namespace nix {
|
|
|
|
|
|
|
|
|
2023-06-19 07:04:59 +03:00
|
|
|
/**
|
|
|
|
* Parse a generation name of the format
|
|
|
|
* `<profilename>-<number>-link'.
|
|
|
|
*/
|
2022-02-25 17:00:00 +02:00
|
|
|
static std::optional<GenerationNumber> parseName(const std::string & profileName, const std::string & name)
|
2004-02-06 18:16:55 +02:00
|
|
|
{
|
2022-02-25 17:00:00 +02:00
|
|
|
if (name.substr(0, profileName.size() + 1) != profileName + "-") return {};
|
|
|
|
auto s = name.substr(profileName.size() + 1);
|
|
|
|
auto p = s.find("-link");
|
|
|
|
if (p == std::string::npos) return {};
|
2021-01-08 13:22:21 +02:00
|
|
|
if (auto n = string2Int<unsigned int>(s.substr(0, p)))
|
|
|
|
return *n;
|
2004-09-10 16:32:08 +03:00
|
|
|
else
|
2020-07-16 16:14:22 +03:00
|
|
|
return {};
|
2004-02-06 18:16:55 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
|
2020-07-16 16:14:22 +03:00
|
|
|
std::pair<Generations, std::optional<GenerationNumber>> findGenerations(Path profile)
|
2004-02-06 16:57:10 +02:00
|
|
|
{
|
2004-02-06 18:03:27 +02:00
|
|
|
Generations gens;
|
|
|
|
|
2004-02-06 16:57:10 +02:00
|
|
|
Path profileDir = dirOf(profile);
|
2019-12-05 20:11:09 +02:00
|
|
|
auto profileName = std::string(baseNameOf(profile));
|
2012-12-03 19:19:49 +02:00
|
|
|
|
2014-08-01 17:37:47 +03:00
|
|
|
for (auto & i : readDirectory(profileDir)) {
|
2020-07-16 16:14:22 +03:00
|
|
|
if (auto n = parseName(profileName, i.name)) {
|
|
|
|
auto path = profileDir + "/" + i.name;
|
|
|
|
gens.push_back({
|
|
|
|
.number = *n,
|
|
|
|
.path = path,
|
2020-09-23 20:17:28 +03:00
|
|
|
.creationTime = lstat(path).st_mtime
|
2020-07-16 16:14:22 +03:00
|
|
|
});
|
2004-02-06 18:03:27 +02:00
|
|
|
}
|
2004-02-06 16:57:10 +02:00
|
|
|
}
|
|
|
|
|
2020-07-16 16:14:22 +03:00
|
|
|
gens.sort([](const Generation & a, const Generation & b)
|
|
|
|
{
|
|
|
|
return a.number < b.number;
|
|
|
|
});
|
2004-02-06 18:03:27 +02:00
|
|
|
|
2020-07-16 16:14:22 +03:00
|
|
|
return {
|
|
|
|
gens,
|
|
|
|
pathExists(profile)
|
2004-02-06 18:16:55 +02:00
|
|
|
? parseName(profileName, readLink(profile))
|
2020-07-16 16:14:22 +03:00
|
|
|
: std::nullopt
|
|
|
|
};
|
2004-02-06 18:03:27 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
|
2023-06-19 07:04:59 +03:00
|
|
|
/**
|
|
|
|
* Create a generation name that can be parsed by `parseName()`.
|
|
|
|
*/
|
|
|
|
static Path makeName(const Path & profile, GenerationNumber num)
|
2004-09-10 16:32:08 +03:00
|
|
|
{
|
2023-06-19 07:04:59 +03:00
|
|
|
return fmt("%s-%s-link", profile, num);
|
2004-09-10 16:32:08 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
|
2023-06-19 07:04:59 +03:00
|
|
|
Path createGeneration(LocalFSStore & store, Path profile, StorePath outPath)
|
2004-02-06 18:03:27 +02:00
|
|
|
{
|
|
|
|
/* The new generation number should be higher than old the
|
|
|
|
previous ones. */
|
2020-07-16 16:14:22 +03:00
|
|
|
auto [gens, dummy] = findGenerations(profile);
|
2015-05-18 09:38:49 +03:00
|
|
|
|
2020-07-16 16:14:22 +03:00
|
|
|
GenerationNumber num;
|
2015-05-18 09:38:49 +03:00
|
|
|
if (gens.size() > 0) {
|
2015-05-19 21:03:36 +03:00
|
|
|
Generation last = gens.back();
|
|
|
|
|
2023-06-19 07:04:59 +03:00
|
|
|
if (readLink(last.path) == store.printStorePath(outPath)) {
|
2015-05-20 18:29:52 +03:00
|
|
|
/* We only create a new generation symlink if it differs
|
|
|
|
from the last one.
|
2015-05-19 21:03:36 +03:00
|
|
|
|
|
|
|
This helps keeping gratuitous installs/rebuilds from piling
|
|
|
|
up uncontrolled numbers of generations, cluttering up the
|
|
|
|
UI like grub. */
|
|
|
|
return last.path;
|
2015-05-18 09:38:49 +03:00
|
|
|
}
|
|
|
|
|
2023-06-19 07:04:59 +03:00
|
|
|
num = last.number;
|
2015-05-18 09:38:49 +03:00
|
|
|
} else {
|
|
|
|
num = 0;
|
|
|
|
}
|
2006-09-15 01:48:59 +03:00
|
|
|
|
2006-09-15 01:30:33 +03:00
|
|
|
/* Create the new generation. Note that addPermRoot() blocks if
|
|
|
|
the garbage collector is running to prevent the stuff we've
|
2006-11-30 20:35:36 +02:00
|
|
|
built from moving from the temporary roots (which the GC knows)
|
2006-09-15 01:30:33 +03:00
|
|
|
to the permanent roots (of which the GC would have a stale
|
|
|
|
view). If we didn't do it this way, the GC might remove the
|
|
|
|
user environment etc. we've just built. */
|
2023-06-19 07:04:59 +03:00
|
|
|
Path generation = makeName(profile, num + 1);
|
|
|
|
store.addPermRoot(outPath, generation);
|
2006-09-15 01:30:33 +03:00
|
|
|
|
|
|
|
return generation;
|
2004-02-06 16:57:10 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
|
2004-09-10 16:32:08 +03:00
|
|
|
static void removeFile(const Path & path)
|
|
|
|
{
|
|
|
|
if (remove(path.c_str()) == -1)
|
2020-04-22 02:07:07 +03:00
|
|
|
throw SysError("cannot unlink '%1%'", path);
|
2004-09-10 16:32:08 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
|
2020-07-16 16:14:22 +03:00
|
|
|
void deleteGeneration(const Path & profile, GenerationNumber gen)
|
2004-09-10 16:32:08 +03:00
|
|
|
{
|
2023-06-19 07:04:59 +03:00
|
|
|
Path generation = makeName(profile, gen);
|
2004-09-10 16:32:08 +03:00
|
|
|
removeFile(generation);
|
|
|
|
}
|
|
|
|
|
2023-06-19 07:04:59 +03:00
|
|
|
/**
|
|
|
|
* Delete a generation with dry-run mode.
|
|
|
|
*
|
|
|
|
* Like `deleteGeneration()` but:
|
|
|
|
*
|
|
|
|
* - We log what we are going to do.
|
|
|
|
*
|
|
|
|
* - We only actually delete if `dryRun` is false.
|
|
|
|
*/
|
2020-07-16 16:14:22 +03:00
|
|
|
static void deleteGeneration2(const Path & profile, GenerationNumber gen, bool dryRun)
|
2015-05-21 17:26:03 +03:00
|
|
|
{
|
|
|
|
if (dryRun)
|
2021-09-14 21:35:12 +03:00
|
|
|
notice("would remove profile version %1%", gen);
|
2015-05-21 17:26:03 +03:00
|
|
|
else {
|
2021-09-14 21:35:12 +03:00
|
|
|
notice("removing profile version %1%", gen);
|
2015-05-21 17:26:03 +03:00
|
|
|
deleteGeneration(profile, gen);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2020-07-16 16:14:22 +03:00
|
|
|
void deleteGenerations(const Path & profile, const std::set<GenerationNumber> & gensToDelete, bool dryRun)
|
2015-05-21 17:26:03 +03:00
|
|
|
{
|
|
|
|
PathLocks lock;
|
|
|
|
lockProfile(lock, profile);
|
|
|
|
|
2020-07-16 16:14:22 +03:00
|
|
|
auto [gens, curGen] = findGenerations(profile);
|
2015-05-21 17:26:03 +03:00
|
|
|
|
2020-07-16 16:14:22 +03:00
|
|
|
if (gensToDelete.count(*curGen))
|
2021-09-14 20:57:45 +03:00
|
|
|
throw Error("cannot delete current version of profile %1%'", profile);
|
2015-05-21 17:26:03 +03:00
|
|
|
|
|
|
|
for (auto & i : gens) {
|
2020-07-16 16:14:22 +03:00
|
|
|
if (!gensToDelete.count(i.number)) continue;
|
2015-05-21 17:26:03 +03:00
|
|
|
deleteGeneration2(profile, i.number, dryRun);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2023-06-19 07:04:59 +03:00
|
|
|
/**
|
|
|
|
* Advanced the iterator until the given predicate `cond` returns `true`.
|
|
|
|
*/
|
|
|
|
static inline void iterDropUntil(Generations & gens, auto && i, auto && cond)
|
|
|
|
{
|
|
|
|
for (; i != gens.rend() && !cond(*i); ++i);
|
|
|
|
}
|
|
|
|
|
2020-07-16 16:14:22 +03:00
|
|
|
void deleteGenerationsGreaterThan(const Path & profile, GenerationNumber max, bool dryRun)
|
2016-01-07 03:15:19 +02:00
|
|
|
{
|
2023-06-19 07:04:59 +03:00
|
|
|
if (max == 0)
|
|
|
|
throw Error("Must keep at least one generation, otherwise the current one would be deleted");
|
|
|
|
|
2016-01-07 03:15:19 +02:00
|
|
|
PathLocks lock;
|
|
|
|
lockProfile(lock, profile);
|
|
|
|
|
2023-06-19 07:04:59 +03:00
|
|
|
auto [gens, _curGen] = findGenerations(profile);
|
|
|
|
auto curGen = _curGen;
|
|
|
|
|
|
|
|
auto i = gens.rbegin();
|
|
|
|
|
|
|
|
// Find the current generation
|
|
|
|
iterDropUntil(gens, i, [&](auto & g) { return g.number == curGen; });
|
|
|
|
|
|
|
|
// Skip over `max` generations, preserving them
|
2023-09-25 19:45:13 +03:00
|
|
|
for (GenerationNumber keep = 0; i != gens.rend() && keep < max; ++i, ++keep);
|
2023-06-19 07:04:59 +03:00
|
|
|
|
|
|
|
// Delete the rest
|
|
|
|
for (; i != gens.rend(); ++i)
|
|
|
|
deleteGeneration2(profile, i->number, dryRun);
|
2016-01-07 03:15:19 +02:00
|
|
|
}
|
2015-05-21 17:26:03 +03:00
|
|
|
|
|
|
|
void deleteOldGenerations(const Path & profile, bool dryRun)
|
|
|
|
{
|
|
|
|
PathLocks lock;
|
|
|
|
lockProfile(lock, profile);
|
|
|
|
|
2020-07-16 16:14:22 +03:00
|
|
|
auto [gens, curGen] = findGenerations(profile);
|
2015-05-21 17:26:03 +03:00
|
|
|
|
|
|
|
for (auto & i : gens)
|
|
|
|
if (i.number != curGen)
|
|
|
|
deleteGeneration2(profile, i.number, dryRun);
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
void deleteGenerationsOlderThan(const Path & profile, time_t t, bool dryRun)
|
|
|
|
{
|
|
|
|
PathLocks lock;
|
|
|
|
lockProfile(lock, profile);
|
|
|
|
|
2020-07-16 16:14:22 +03:00
|
|
|
auto [gens, curGen] = findGenerations(profile);
|
2015-05-21 17:26:03 +03:00
|
|
|
|
2023-06-19 07:04:59 +03:00
|
|
|
auto i = gens.rbegin();
|
|
|
|
|
|
|
|
// Predicate that the generation is older than the given time.
|
|
|
|
auto older = [&](auto & g) { return g.creationTime < t; };
|
|
|
|
|
|
|
|
// Find the first older generation, if one exists
|
|
|
|
iterDropUntil(gens, i, older);
|
|
|
|
|
|
|
|
/* Take the previous generation
|
|
|
|
|
|
|
|
We don't want delete this one yet because it
|
|
|
|
existed at the requested point in time, and
|
|
|
|
we want to be able to roll back to it. */
|
|
|
|
if (i != gens.rend()) ++i;
|
|
|
|
|
|
|
|
// Delete all previous generations (unless current).
|
|
|
|
for (; i != gens.rend(); ++i) {
|
|
|
|
/* Creating date and generations should be monotonic, so lower
|
|
|
|
numbered derivations should also be older. */
|
|
|
|
assert(older(*i));
|
|
|
|
if (i->number != curGen)
|
|
|
|
deleteGeneration2(profile, i->number, dryRun);
|
|
|
|
}
|
2015-05-21 17:26:03 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
|
2023-06-19 07:04:59 +03:00
|
|
|
time_t parseOlderThanTimeSpec(std::string_view timeSpec)
|
2015-05-21 17:26:03 +03:00
|
|
|
{
|
2021-07-11 13:15:06 +03:00
|
|
|
if (timeSpec.empty() || timeSpec[timeSpec.size() - 1] != 'd')
|
2021-07-12 16:04:46 +03:00
|
|
|
throw UsageError("invalid number of days specifier '%1%', expected something like '14d'", timeSpec);
|
2021-07-11 13:15:06 +03:00
|
|
|
|
2015-05-21 17:26:03 +03:00
|
|
|
time_t curTime = time(0);
|
2022-02-25 17:00:00 +02:00
|
|
|
auto strDays = timeSpec.substr(0, timeSpec.size() - 1);
|
2021-01-08 13:22:21 +02:00
|
|
|
auto days = string2Int<int>(strDays);
|
2015-05-21 17:26:03 +03:00
|
|
|
|
2021-01-08 13:22:21 +02:00
|
|
|
if (!days || *days < 1)
|
2021-07-12 16:04:46 +03:00
|
|
|
throw UsageError("invalid number of days specifier '%1%'", timeSpec);
|
2015-05-21 17:26:03 +03:00
|
|
|
|
2023-06-19 07:04:59 +03:00
|
|
|
return curTime - *days * 24 * 3600;
|
2015-05-21 17:26:03 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
|
2004-02-06 16:57:10 +02:00
|
|
|
void switchLink(Path link, Path target)
|
|
|
|
{
|
|
|
|
/* Hacky. */
|
|
|
|
if (dirOf(target) == dirOf(link)) target = baseNameOf(target);
|
2012-12-03 19:19:49 +02:00
|
|
|
|
2015-04-09 12:42:04 +03:00
|
|
|
replaceSymlink(target, link);
|
2004-02-06 16:57:10 +02:00
|
|
|
}
|
2006-09-05 00:06:23 +03:00
|
|
|
|
2010-04-21 18:08:58 +03:00
|
|
|
|
2021-09-14 20:05:28 +03:00
|
|
|
void switchGeneration(
|
|
|
|
const Path & profile,
|
|
|
|
std::optional<GenerationNumber> dstGen,
|
|
|
|
bool dryRun)
|
|
|
|
{
|
|
|
|
PathLocks lock;
|
|
|
|
lockProfile(lock, profile);
|
|
|
|
|
|
|
|
auto [gens, curGen] = findGenerations(profile);
|
|
|
|
|
|
|
|
std::optional<Generation> dst;
|
|
|
|
for (auto & i : gens)
|
|
|
|
if ((!dstGen && i.number < curGen) ||
|
|
|
|
(dstGen && i.number == *dstGen))
|
|
|
|
dst = i;
|
|
|
|
|
|
|
|
if (!dst) {
|
|
|
|
if (dstGen)
|
2021-09-14 20:57:45 +03:00
|
|
|
throw Error("profile version %1% does not exist", *dstGen);
|
2021-09-14 20:05:28 +03:00
|
|
|
else
|
2021-09-14 20:57:45 +03:00
|
|
|
throw Error("no profile version older than the current (%1%) exists", curGen.value_or(0));
|
2021-09-14 20:05:28 +03:00
|
|
|
}
|
|
|
|
|
2021-09-14 20:57:45 +03:00
|
|
|
notice("switching profile from version %d to %d", curGen.value_or(0), dst->number);
|
2021-09-14 20:05:28 +03:00
|
|
|
|
|
|
|
if (dryRun) return;
|
|
|
|
|
|
|
|
switchLink(profile, dst->path);
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2010-04-21 18:08:58 +03:00
|
|
|
void lockProfile(PathLocks & lock, const Path & profile)
|
|
|
|
{
|
2023-03-02 16:44:19 +02:00
|
|
|
lock.lockPaths({profile}, fmt("waiting for lock on profile '%1%'", profile));
|
2010-04-21 18:08:58 +03:00
|
|
|
lock.setDeletion(true);
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2022-02-25 17:00:00 +02:00
|
|
|
std::string optimisticLockProfile(const Path & profile)
|
2010-04-21 18:08:58 +03:00
|
|
|
{
|
|
|
|
return pathExists(profile) ? readLink(profile) : "";
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2022-04-13 11:26:50 +03:00
|
|
|
Path profilesDir()
|
|
|
|
{
|
2023-03-21 14:37:19 +02:00
|
|
|
auto profileRoot =
|
|
|
|
(getuid() == 0)
|
|
|
|
? rootProfilesDir()
|
|
|
|
: createNixStateDir() + "/profiles";
|
2022-04-13 11:26:50 +03:00
|
|
|
createDirs(profileRoot);
|
|
|
|
return profileRoot;
|
|
|
|
}
|
|
|
|
|
2023-03-21 14:37:19 +02:00
|
|
|
Path rootProfilesDir()
|
|
|
|
{
|
|
|
|
return settings.nixStateDir + "/profiles/per-user/root";
|
|
|
|
}
|
|
|
|
|
2022-04-13 11:26:50 +03:00
|
|
|
|
2020-03-24 15:26:13 +02:00
|
|
|
Path getDefaultProfile()
|
|
|
|
{
|
2021-11-17 22:35:21 +02:00
|
|
|
Path profileLink = settings.useXDGBaseDirectories ? createNixStateDir() + "/profile" : getHome() + "/.nix-profile";
|
2020-03-24 15:26:13 +02:00
|
|
|
try {
|
2023-03-21 14:37:19 +02:00
|
|
|
auto profile = profilesDir() + "/profile";
|
2023-01-17 15:15:00 +02:00
|
|
|
if (!pathExists(profileLink)) {
|
|
|
|
replaceSymlink(profile, profileLink);
|
2020-03-24 15:26:13 +02:00
|
|
|
}
|
2023-03-21 14:37:19 +02:00
|
|
|
// Backwards compatibiliy measure: Make root's profile available as
|
|
|
|
// `.../default` as it's what NixOS and most of the init scripts expect
|
|
|
|
Path globalProfileLink = settings.nixStateDir + "/profiles/default";
|
|
|
|
if (getuid() == 0 && !pathExists(globalProfileLink)) {
|
|
|
|
replaceSymlink(profile, globalProfileLink);
|
|
|
|
}
|
2020-03-24 15:26:13 +02:00
|
|
|
return absPath(readLink(profileLink), dirOf(profileLink));
|
|
|
|
} catch (Error &) {
|
|
|
|
return profileLink;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2023-03-21 14:37:19 +02:00
|
|
|
Path defaultChannelsDir()
|
|
|
|
{
|
|
|
|
return profilesDir() + "/channels";
|
|
|
|
}
|
|
|
|
|
|
|
|
Path rootChannelsDir()
|
|
|
|
{
|
|
|
|
return rootProfilesDir() + "/channels";
|
|
|
|
}
|
2020-03-24 15:26:13 +02:00
|
|
|
|
2006-09-05 00:06:23 +03:00
|
|
|
}
|