2022-04-13 11:26:50 +03:00
|
|
|
#include "profiles.hh"
|
2016-08-11 18:34:43 +03:00
|
|
|
#include "shared.hh"
|
|
|
|
#include "globals.hh"
|
2020-04-07 00:57:28 +03:00
|
|
|
#include "filetransfer.hh"
|
2018-10-26 12:35:46 +03:00
|
|
|
#include "store-api.hh"
|
2021-01-26 13:22:24 +02:00
|
|
|
#include "legacy.hh"
|
2023-07-24 21:02:05 +03:00
|
|
|
#include "eval-settings.hh" // for defexpr
|
2023-10-25 07:43:36 +03:00
|
|
|
#include "users.hh"
|
2023-10-20 20:50:21 +03:00
|
|
|
#include "tarball.hh"
|
2018-10-26 12:35:46 +03:00
|
|
|
|
2016-08-11 18:34:43 +03:00
|
|
|
#include <fcntl.h>
|
|
|
|
#include <regex>
|
|
|
|
#include <pwd.h>
|
|
|
|
|
|
|
|
using namespace nix;
|
|
|
|
|
2022-02-25 17:00:00 +02:00
|
|
|
typedef std::map<std::string, std::string> Channels;
|
2016-08-11 18:34:43 +03:00
|
|
|
|
2018-03-20 18:33:45 +02:00
|
|
|
static Channels channels;
|
|
|
|
static Path channelsList;
|
2016-08-11 18:34:43 +03:00
|
|
|
|
|
|
|
// Reads the list of channels.
|
|
|
|
static void readChannels()
|
|
|
|
{
|
|
|
|
if (!pathExists(channelsList)) return;
|
|
|
|
auto channelsFile = readFile(channelsList);
|
|
|
|
|
2022-02-25 17:00:00 +02:00
|
|
|
for (const auto & line : tokenizeString<std::vector<std::string>>(channelsFile, "\n")) {
|
2016-08-11 18:34:43 +03:00
|
|
|
chomp(line);
|
|
|
|
if (std::regex_search(line, std::regex("^\\s*\\#")))
|
|
|
|
continue;
|
2022-02-25 17:00:00 +02:00
|
|
|
auto split = tokenizeString<std::vector<std::string>>(line, " ");
|
2016-08-11 18:34:43 +03:00
|
|
|
auto url = std::regex_replace(split[0], std::regex("/*$"), "");
|
2019-12-05 20:11:09 +02:00
|
|
|
auto name = split.size() > 1 ? split[1] : std::string(baseNameOf(url));
|
2016-08-11 18:34:43 +03:00
|
|
|
channels[name] = url;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// Writes the list of channels.
|
|
|
|
static void writeChannels()
|
|
|
|
{
|
|
|
|
auto channelsFD = AutoCloseFD{open(channelsList.c_str(), O_WRONLY | O_CLOEXEC | O_CREAT | O_TRUNC, 0644)};
|
|
|
|
if (!channelsFD)
|
2020-05-01 02:56:26 +03:00
|
|
|
throw SysError("opening '%1%' for writing", channelsList);
|
2016-08-11 18:34:43 +03:00
|
|
|
for (const auto & channel : channels)
|
|
|
|
writeFull(channelsFD.get(), channel.second + " " + channel.first + "\n");
|
|
|
|
}
|
|
|
|
|
|
|
|
// Adds a channel.
|
2022-02-25 17:00:00 +02:00
|
|
|
static void addChannel(const std::string & url, const std::string & name)
|
2016-08-11 18:34:43 +03:00
|
|
|
{
|
|
|
|
if (!regex_search(url, std::regex("^(file|http|https)://")))
|
2020-05-01 02:56:26 +03:00
|
|
|
throw Error("invalid channel URL '%1%'", url);
|
2016-08-11 18:34:43 +03:00
|
|
|
if (!regex_search(name, std::regex("^[a-zA-Z0-9_][a-zA-Z0-9_\\.-]*$")))
|
2020-05-01 02:56:26 +03:00
|
|
|
throw Error("invalid channel identifier '%1%'", name);
|
2016-08-11 18:34:43 +03:00
|
|
|
readChannels();
|
|
|
|
channels[name] = url;
|
|
|
|
writeChannels();
|
|
|
|
}
|
|
|
|
|
2018-03-20 18:33:45 +02:00
|
|
|
static Path profile;
|
2016-08-11 18:34:43 +03:00
|
|
|
|
|
|
|
// Remove a channel.
|
2022-02-25 17:00:00 +02:00
|
|
|
static void removeChannel(const std::string & name)
|
2016-08-11 18:34:43 +03:00
|
|
|
{
|
|
|
|
readChannels();
|
|
|
|
channels.erase(name);
|
|
|
|
writeChannels();
|
|
|
|
|
|
|
|
runProgram(settings.nixBinDir + "/nix-env", true, { "--profile", profile, "--uninstall", name });
|
|
|
|
}
|
|
|
|
|
2018-03-20 18:33:45 +02:00
|
|
|
static Path nixDefExpr;
|
2016-08-11 18:34:43 +03:00
|
|
|
|
|
|
|
// Fetch Nix expressions and binary cache URLs from the subscribed channels.
|
|
|
|
static void update(const StringSet & channelNames)
|
|
|
|
{
|
|
|
|
readChannels();
|
|
|
|
|
|
|
|
auto store = openStore();
|
|
|
|
|
2020-09-17 10:35:30 +03:00
|
|
|
auto [fd, unpackChannelPath] = createTempFile();
|
|
|
|
writeFull(fd.get(),
|
|
|
|
#include "unpack-channel.nix.gen.hh"
|
|
|
|
);
|
|
|
|
fd = -1;
|
|
|
|
AutoDelete del(unpackChannelPath, false);
|
|
|
|
|
2016-08-11 18:34:43 +03:00
|
|
|
// Download each channel.
|
2018-03-20 18:33:45 +02:00
|
|
|
Strings exprs;
|
2016-08-11 18:34:43 +03:00
|
|
|
for (const auto & channel : channels) {
|
|
|
|
auto name = channel.first;
|
|
|
|
auto url = channel.second;
|
|
|
|
|
|
|
|
// If the URL contains a version number, append it to the name
|
|
|
|
// attribute (so that "nix-env -q" on the channels profile
|
|
|
|
// shows something useful).
|
|
|
|
auto cname = name;
|
|
|
|
std::smatch match;
|
2019-12-05 20:11:09 +02:00
|
|
|
auto urlBase = std::string(baseNameOf(url));
|
|
|
|
if (std::regex_search(urlBase, match, std::regex("(-\\d.*)$")))
|
2022-03-03 12:11:16 +02:00
|
|
|
cname = cname + match.str(1);
|
2016-08-11 18:34:43 +03:00
|
|
|
|
2018-03-20 18:33:45 +02:00
|
|
|
std::string extraAttrs;
|
2016-08-11 18:34:43 +03:00
|
|
|
|
2021-07-25 15:00:00 +03:00
|
|
|
if (!(channelNames.empty() || channelNames.count(name))) {
|
|
|
|
// no need to update this channel, reuse the existing store path
|
|
|
|
Path symlink = profile + "/" + name;
|
|
|
|
Path storepath = dirOf(readLink(symlink));
|
|
|
|
exprs.push_back("f: rec { name = \"" + cname + "\"; type = \"derivation\"; outputs = [\"out\"]; system = \"builtin\"; outPath = builtins.storePath \"" + storepath + "\"; out = { inherit outPath; };}");
|
|
|
|
} else {
|
|
|
|
// We want to download the url to a file to see if it's a tarball while also checking if we
|
|
|
|
// got redirected in the process, so that we can grab the various parts of a nix channel
|
|
|
|
// definition from a consistent location if the redirect changes mid-download.
|
|
|
|
auto result = fetchers::downloadFile(store, url, std::string(baseNameOf(url)), false);
|
|
|
|
auto filename = store->toRealPath(result.storePath);
|
|
|
|
url = result.effectiveUrl;
|
|
|
|
|
|
|
|
bool unpacked = false;
|
|
|
|
if (std::regex_search(filename, std::regex("\\.tar\\.(gz|bz2|xz)$"))) {
|
|
|
|
runProgram(settings.nixBinDir + "/nix-build", false, { "--no-out-link", "--expr", "import " + unpackChannelPath +
|
|
|
|
"{ name = \"" + cname + "\"; channelName = \"" + name + "\"; src = builtins.storePath \"" + filename + "\"; }" });
|
|
|
|
unpacked = true;
|
|
|
|
}
|
2016-08-11 18:34:43 +03:00
|
|
|
|
2021-07-25 15:00:00 +03:00
|
|
|
if (!unpacked) {
|
|
|
|
// Download the channel tarball.
|
|
|
|
try {
|
|
|
|
filename = store->toRealPath(fetchers::downloadFile(store, url + "/nixexprs.tar.xz", "nixexprs.tar.xz", false).storePath);
|
|
|
|
} catch (FileTransferError & e) {
|
|
|
|
filename = store->toRealPath(fetchers::downloadFile(store, url + "/nixexprs.tar.bz2", "nixexprs.tar.bz2", false).storePath);
|
|
|
|
}
|
2016-08-11 18:34:43 +03:00
|
|
|
}
|
2021-07-25 15:00:00 +03:00
|
|
|
// Regardless of where it came from, add the expression representing this channel to accumulated expression
|
|
|
|
exprs.push_back("f: f { name = \"" + cname + "\"; channelName = \"" + name + "\"; src = builtins.storePath \"" + filename + "\"; " + extraAttrs + " }");
|
2016-08-11 18:34:43 +03:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// Unpack the channel tarballs into the Nix store and install them
|
|
|
|
// into the channels profile.
|
2024-02-21 02:17:18 +02:00
|
|
|
std::cerr << "unpacking " << exprs.size() << " channels...\n";
|
2021-07-25 15:00:00 +03:00
|
|
|
Strings envArgs{ "--profile", profile, "--file", unpackChannelPath, "--install", "--remove-all", "--from-expression" };
|
2016-08-11 18:34:43 +03:00
|
|
|
for (auto & expr : exprs)
|
|
|
|
envArgs.push_back(std::move(expr));
|
|
|
|
envArgs.push_back("--quiet");
|
|
|
|
runProgram(settings.nixBinDir + "/nix-env", false, envArgs);
|
|
|
|
|
|
|
|
// Make the channels appear in nix-env.
|
|
|
|
struct stat st;
|
|
|
|
if (lstat(nixDefExpr.c_str(), &st) == 0) {
|
|
|
|
if (S_ISLNK(st.st_mode))
|
|
|
|
// old-skool ~/.nix-defexpr
|
|
|
|
if (unlink(nixDefExpr.c_str()) == -1)
|
2020-05-01 02:56:26 +03:00
|
|
|
throw SysError("unlinking %1%", nixDefExpr);
|
2016-08-11 18:34:43 +03:00
|
|
|
} else if (errno != ENOENT) {
|
2020-05-01 02:56:26 +03:00
|
|
|
throw SysError("getting status of %1%", nixDefExpr);
|
2016-08-11 18:34:43 +03:00
|
|
|
}
|
|
|
|
createDirs(nixDefExpr);
|
|
|
|
auto channelLink = nixDefExpr + "/channels";
|
|
|
|
replaceSymlink(profile, channelLink);
|
|
|
|
}
|
|
|
|
|
2020-10-06 14:36:55 +03:00
|
|
|
static int main_nix_channel(int argc, char ** argv)
|
2016-08-11 18:34:43 +03:00
|
|
|
{
|
2018-10-26 12:35:46 +03:00
|
|
|
{
|
2016-08-11 18:34:43 +03:00
|
|
|
// Figure out the name of the `.nix-channels' file to use
|
2017-05-05 17:40:12 +03:00
|
|
|
auto home = getHome();
|
2021-11-17 22:35:21 +02:00
|
|
|
channelsList = settings.useXDGBaseDirectories ? createNixStateDir() + "/channels" : home + "/.nix-channels";
|
2023-07-24 21:02:05 +03:00
|
|
|
nixDefExpr = getNixDefExpr();
|
2016-08-11 18:34:43 +03:00
|
|
|
|
|
|
|
// Figure out the name of the channels profile.
|
2023-03-21 14:37:19 +02:00
|
|
|
profile = profilesDir() + "/channels";
|
2023-03-20 11:24:48 +02:00
|
|
|
createDirs(dirOf(profile));
|
2016-08-11 18:34:43 +03:00
|
|
|
|
|
|
|
enum {
|
|
|
|
cNone,
|
|
|
|
cAdd,
|
|
|
|
cRemove,
|
|
|
|
cList,
|
|
|
|
cUpdate,
|
2021-05-13 20:33:03 +03:00
|
|
|
cListGenerations,
|
2016-08-11 18:34:43 +03:00
|
|
|
cRollback
|
|
|
|
} cmd = cNone;
|
2022-02-25 17:00:00 +02:00
|
|
|
std::vector<std::string> args;
|
2016-08-11 18:34:43 +03:00
|
|
|
parseCmdLine(argc, argv, [&](Strings::iterator & arg, const Strings::iterator & end) {
|
|
|
|
if (*arg == "--help") {
|
|
|
|
showManPage("nix-channel");
|
|
|
|
} else if (*arg == "--version") {
|
|
|
|
printVersion("nix-channel");
|
|
|
|
} else if (*arg == "--add") {
|
|
|
|
cmd = cAdd;
|
|
|
|
} else if (*arg == "--remove") {
|
|
|
|
cmd = cRemove;
|
|
|
|
} else if (*arg == "--list") {
|
|
|
|
cmd = cList;
|
|
|
|
} else if (*arg == "--update") {
|
|
|
|
cmd = cUpdate;
|
2021-05-13 20:33:03 +03:00
|
|
|
} else if (*arg == "--list-generations") {
|
|
|
|
cmd = cListGenerations;
|
2016-08-11 18:34:43 +03:00
|
|
|
} else if (*arg == "--rollback") {
|
|
|
|
cmd = cRollback;
|
|
|
|
} else {
|
2020-03-30 17:04:18 +03:00
|
|
|
if (hasPrefix(*arg, "-"))
|
|
|
|
throw UsageError("unsupported argument '%s'", *arg);
|
2016-08-11 18:34:43 +03:00
|
|
|
args.push_back(std::move(*arg));
|
|
|
|
}
|
|
|
|
return true;
|
|
|
|
});
|
2018-02-08 18:26:18 +02:00
|
|
|
|
2016-08-11 18:34:43 +03:00
|
|
|
switch (cmd) {
|
|
|
|
case cNone:
|
2020-05-01 02:56:26 +03:00
|
|
|
throw UsageError("no command specified");
|
2016-08-11 18:34:43 +03:00
|
|
|
case cAdd:
|
|
|
|
if (args.size() < 1 || args.size() > 2)
|
2020-05-01 02:56:26 +03:00
|
|
|
throw UsageError("'--add' requires one or two arguments");
|
2016-08-11 18:34:43 +03:00
|
|
|
{
|
|
|
|
auto url = args[0];
|
2018-03-20 18:33:45 +02:00
|
|
|
std::string name;
|
2016-08-11 18:34:43 +03:00
|
|
|
if (args.size() == 2) {
|
|
|
|
name = args[1];
|
|
|
|
} else {
|
|
|
|
name = baseNameOf(url);
|
|
|
|
name = std::regex_replace(name, std::regex("-unstable$"), "");
|
|
|
|
name = std::regex_replace(name, std::regex("-stable$"), "");
|
|
|
|
}
|
|
|
|
addChannel(url, name);
|
|
|
|
}
|
|
|
|
break;
|
|
|
|
case cRemove:
|
|
|
|
if (args.size() != 1)
|
2020-05-01 02:56:26 +03:00
|
|
|
throw UsageError("'--remove' requires one argument");
|
2016-08-11 18:34:43 +03:00
|
|
|
removeChannel(args[0]);
|
|
|
|
break;
|
|
|
|
case cList:
|
|
|
|
if (!args.empty())
|
2020-05-01 02:56:26 +03:00
|
|
|
throw UsageError("'--list' expects no arguments");
|
2016-08-11 18:34:43 +03:00
|
|
|
readChannels();
|
|
|
|
for (const auto & channel : channels)
|
|
|
|
std::cout << channel.first << ' ' << channel.second << '\n';
|
|
|
|
break;
|
|
|
|
case cUpdate:
|
|
|
|
update(StringSet(args.begin(), args.end()));
|
|
|
|
break;
|
2021-05-13 20:33:03 +03:00
|
|
|
case cListGenerations:
|
|
|
|
if (!args.empty())
|
|
|
|
throw UsageError("'--list-generations' expects no arguments");
|
|
|
|
std::cout << runProgram(settings.nixBinDir + "/nix-env", false, {"--profile", profile, "--list-generations"}) << std::flush;
|
|
|
|
break;
|
2016-08-11 18:34:43 +03:00
|
|
|
case cRollback:
|
|
|
|
if (args.size() > 1)
|
2020-05-01 02:56:26 +03:00
|
|
|
throw UsageError("'--rollback' has at most one argument");
|
2018-03-20 18:33:45 +02:00
|
|
|
Strings envArgs{"--profile", profile};
|
2016-08-11 18:34:43 +03:00
|
|
|
if (args.size() == 1) {
|
|
|
|
envArgs.push_back("--switch-generation");
|
|
|
|
envArgs.push_back(args[0]);
|
|
|
|
} else {
|
|
|
|
envArgs.push_back("--rollback");
|
|
|
|
}
|
|
|
|
runProgram(settings.nixBinDir + "/nix-env", false, envArgs);
|
|
|
|
break;
|
|
|
|
}
|
2018-10-26 12:35:46 +03:00
|
|
|
|
|
|
|
return 0;
|
|
|
|
}
|
2016-08-11 18:34:43 +03:00
|
|
|
}
|
2018-10-26 12:35:46 +03:00
|
|
|
|
2020-10-06 14:36:55 +03:00
|
|
|
static RegisterLegacyCommand r_nix_channel("nix-channel", main_nix_channel);
|