2016-08-11 15:34:43 +00:00
|
|
|
#include "shared.hh"
|
|
|
|
#include "globals.hh"
|
|
|
|
#include "download.hh"
|
2018-10-26 09:35:46 +00:00
|
|
|
#include "store-api.hh"
|
2020-03-30 12:29:29 +00:00
|
|
|
#include "../nix/legacy.hh"
|
2020-03-30 14:04:18 +00:00
|
|
|
#include "fetchers.hh"
|
2018-10-26 09:35:46 +00:00
|
|
|
|
2016-08-11 15:34:43 +00:00
|
|
|
#include <fcntl.h>
|
|
|
|
#include <regex>
|
|
|
|
#include <pwd.h>
|
|
|
|
|
|
|
|
using namespace nix;
|
|
|
|
|
|
|
|
typedef std::map<string,string> Channels;
|
|
|
|
|
2018-03-20 16:33:45 +00:00
|
|
|
static Channels channels;
|
|
|
|
static Path channelsList;
|
2016-08-11 15:34:43 +00:00
|
|
|
|
|
|
|
// Reads the list of channels.
|
|
|
|
static void readChannels()
|
|
|
|
{
|
|
|
|
if (!pathExists(channelsList)) return;
|
|
|
|
auto channelsFile = readFile(channelsList);
|
|
|
|
|
|
|
|
for (const auto & line : tokenizeString<std::vector<string>>(channelsFile, "\n")) {
|
|
|
|
chomp(line);
|
|
|
|
if (std::regex_search(line, std::regex("^\\s*\\#")))
|
|
|
|
continue;
|
|
|
|
auto split = tokenizeString<std::vector<string>>(line, " ");
|
|
|
|
auto url = std::regex_replace(split[0], std::regex("/*$"), "");
|
2019-12-05 18:11:09 +00:00
|
|
|
auto name = split.size() > 1 ? split[1] : std::string(baseNameOf(url));
|
2016-08-11 15:34:43 +00: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)
|
2017-07-30 11:27:57 +00:00
|
|
|
throw SysError(format("opening '%1%' for writing") % channelsList);
|
2016-08-11 15:34:43 +00:00
|
|
|
for (const auto & channel : channels)
|
|
|
|
writeFull(channelsFD.get(), channel.second + " " + channel.first + "\n");
|
|
|
|
}
|
|
|
|
|
|
|
|
// Adds a channel.
|
|
|
|
static void addChannel(const string & url, const string & name)
|
|
|
|
{
|
|
|
|
if (!regex_search(url, std::regex("^(file|http|https)://")))
|
2017-07-30 11:27:57 +00:00
|
|
|
throw Error(format("invalid channel URL '%1%'") % url);
|
2016-08-11 15:34:43 +00:00
|
|
|
if (!regex_search(name, std::regex("^[a-zA-Z0-9_][a-zA-Z0-9_\\.-]*$")))
|
2017-07-30 11:27:57 +00:00
|
|
|
throw Error(format("invalid channel identifier '%1%'") % name);
|
2016-08-11 15:34:43 +00:00
|
|
|
readChannels();
|
|
|
|
channels[name] = url;
|
|
|
|
writeChannels();
|
|
|
|
}
|
|
|
|
|
2018-03-20 16:33:45 +00:00
|
|
|
static Path profile;
|
2016-08-11 15:34:43 +00:00
|
|
|
|
|
|
|
// Remove a channel.
|
|
|
|
static void removeChannel(const string & name)
|
|
|
|
{
|
|
|
|
readChannels();
|
|
|
|
channels.erase(name);
|
|
|
|
writeChannels();
|
|
|
|
|
|
|
|
runProgram(settings.nixBinDir + "/nix-env", true, { "--profile", profile, "--uninstall", name });
|
|
|
|
}
|
|
|
|
|
2018-03-20 16:33:45 +00:00
|
|
|
static Path nixDefExpr;
|
2016-08-11 15:34:43 +00:00
|
|
|
|
|
|
|
// Fetch Nix expressions and binary cache URLs from the subscribed channels.
|
|
|
|
static void update(const StringSet & channelNames)
|
|
|
|
{
|
|
|
|
readChannels();
|
|
|
|
|
|
|
|
auto store = openStore();
|
|
|
|
|
|
|
|
// Download each channel.
|
2018-03-20 16:33:45 +00:00
|
|
|
Strings exprs;
|
2016-08-11 15:34:43 +00:00
|
|
|
for (const auto & channel : channels) {
|
|
|
|
auto name = channel.first;
|
|
|
|
auto url = channel.second;
|
2016-11-21 14:54:19 +00:00
|
|
|
if (!(channelNames.empty() || channelNames.count(name)))
|
|
|
|
continue;
|
2016-08-11 15:34:43 +00:00
|
|
|
|
|
|
|
// 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.
|
2020-03-30 14:04:18 +00:00
|
|
|
auto result = fetchers::downloadFile(store, url, std::string(baseNameOf(url)), false);
|
|
|
|
auto filename = store->toRealPath(result.storePath);
|
|
|
|
url = result.effectiveUrl;
|
2016-08-11 15:34:43 +00:00
|
|
|
|
|
|
|
// 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 18:11:09 +00:00
|
|
|
auto urlBase = std::string(baseNameOf(url));
|
|
|
|
if (std::regex_search(urlBase, match, std::regex("(-\\d.*)$")))
|
2016-08-11 15:34:43 +00:00
|
|
|
cname = cname + (string) match[1];
|
|
|
|
|
2018-03-20 16:33:45 +00:00
|
|
|
std::string extraAttrs;
|
2016-08-11 15:34:43 +00:00
|
|
|
|
2018-03-20 16:33:45 +00:00
|
|
|
bool unpacked = false;
|
2016-08-11 15:34:43 +00:00
|
|
|
if (std::regex_search(filename, std::regex("\\.tar\\.(gz|bz2|xz)$"))) {
|
2017-04-10 22:16:36 +00:00
|
|
|
runProgram(settings.nixBinDir + "/nix-build", false, { "--no-out-link", "--expr", "import <nix/unpack-channel.nix> "
|
|
|
|
"{ name = \"" + cname + "\"; channelName = \"" + name + "\"; src = builtins.storePath \"" + filename + "\"; }" });
|
|
|
|
unpacked = true;
|
2016-08-11 15:34:43 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
if (!unpacked) {
|
|
|
|
// Download the channel tarball.
|
|
|
|
try {
|
2020-03-30 14:04:18 +00:00
|
|
|
filename = store->toRealPath(fetchers::downloadFile(store, url + "/nixexprs.tar.xz", "nixexprs.tar.xz", false).storePath);
|
2016-08-11 15:34:43 +00:00
|
|
|
} catch (DownloadError & e) {
|
2020-03-30 14:04:18 +00:00
|
|
|
filename = store->toRealPath(fetchers::downloadFile(store, url + "/nixexprs.tar.bz2", "nixexprs.tar.bz2", false).storePath);
|
2016-08-11 15:34:43 +00: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 + " }");
|
|
|
|
}
|
|
|
|
|
|
|
|
// Unpack the channel tarballs into the Nix store and install them
|
|
|
|
// into the channels profile.
|
|
|
|
std::cerr << "unpacking channels...\n";
|
2018-03-20 16:33:45 +00:00
|
|
|
Strings envArgs{ "--profile", profile, "--file", "<nix/unpack-channel.nix>", "--install", "--from-expression" };
|
2016-08-11 15:34:43 +00: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)
|
|
|
|
throw SysError(format("unlinking %1%") % nixDefExpr);
|
|
|
|
} else if (errno != ENOENT) {
|
|
|
|
throw SysError(format("getting status of %1%") % nixDefExpr);
|
|
|
|
}
|
|
|
|
createDirs(nixDefExpr);
|
|
|
|
auto channelLink = nixDefExpr + "/channels";
|
|
|
|
replaceSymlink(profile, channelLink);
|
|
|
|
}
|
|
|
|
|
2018-10-26 09:35:46 +00:00
|
|
|
static int _main(int argc, char ** argv)
|
2016-08-11 15:34:43 +00:00
|
|
|
{
|
2018-10-26 09:35:46 +00:00
|
|
|
{
|
2016-08-11 15:34:43 +00:00
|
|
|
// Figure out the name of the `.nix-channels' file to use
|
2017-05-05 14:40:12 +00:00
|
|
|
auto home = getHome();
|
2016-08-11 15:34:43 +00:00
|
|
|
channelsList = home + "/.nix-channels";
|
|
|
|
nixDefExpr = home + "/.nix-defexpr";
|
|
|
|
|
|
|
|
// Figure out the name of the channels profile.
|
2019-10-09 17:21:07 +00:00
|
|
|
profile = fmt("%s/profiles/per-user/%s/channels", settings.nixStateDir, getUserName());
|
2016-08-11 15:34:43 +00:00
|
|
|
|
|
|
|
enum {
|
|
|
|
cNone,
|
|
|
|
cAdd,
|
|
|
|
cRemove,
|
|
|
|
cList,
|
|
|
|
cUpdate,
|
|
|
|
cRollback
|
|
|
|
} cmd = cNone;
|
2018-03-20 16:33:45 +00:00
|
|
|
std::vector<string> args;
|
2016-08-11 15:34:43 +00: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;
|
|
|
|
} else if (*arg == "--rollback") {
|
|
|
|
cmd = cRollback;
|
|
|
|
} else {
|
2020-03-30 14:04:18 +00:00
|
|
|
if (hasPrefix(*arg, "-"))
|
|
|
|
throw UsageError("unsupported argument '%s'", *arg);
|
2016-08-11 15:34:43 +00:00
|
|
|
args.push_back(std::move(*arg));
|
|
|
|
}
|
|
|
|
return true;
|
|
|
|
});
|
2018-02-08 16:26:18 +00:00
|
|
|
|
|
|
|
initPlugins();
|
|
|
|
|
2016-08-11 15:34:43 +00:00
|
|
|
switch (cmd) {
|
|
|
|
case cNone:
|
|
|
|
throw UsageError("no command specified");
|
|
|
|
case cAdd:
|
|
|
|
if (args.size() < 1 || args.size() > 2)
|
2017-07-30 11:27:57 +00:00
|
|
|
throw UsageError("'--add' requires one or two arguments");
|
2016-08-11 15:34:43 +00:00
|
|
|
{
|
|
|
|
auto url = args[0];
|
2018-03-20 16:33:45 +00:00
|
|
|
std::string name;
|
2016-08-11 15:34:43 +00: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)
|
2017-07-30 11:27:57 +00:00
|
|
|
throw UsageError("'--remove' requires one argument");
|
2016-08-11 15:34:43 +00:00
|
|
|
removeChannel(args[0]);
|
|
|
|
break;
|
|
|
|
case cList:
|
|
|
|
if (!args.empty())
|
2017-07-30 11:27:57 +00:00
|
|
|
throw UsageError("'--list' expects no arguments");
|
2016-08-11 15:34:43 +00:00
|
|
|
readChannels();
|
|
|
|
for (const auto & channel : channels)
|
|
|
|
std::cout << channel.first << ' ' << channel.second << '\n';
|
|
|
|
break;
|
|
|
|
case cUpdate:
|
|
|
|
update(StringSet(args.begin(), args.end()));
|
|
|
|
break;
|
|
|
|
case cRollback:
|
|
|
|
if (args.size() > 1)
|
2017-07-30 11:27:57 +00:00
|
|
|
throw UsageError("'--rollback' has at most one argument");
|
2018-03-20 16:33:45 +00:00
|
|
|
Strings envArgs{"--profile", profile};
|
2016-08-11 15:34:43 +00: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 09:35:46 +00:00
|
|
|
|
|
|
|
return 0;
|
|
|
|
}
|
2016-08-11 15:34:43 +00:00
|
|
|
}
|
2018-10-26 09:35:46 +00:00
|
|
|
|
|
|
|
static RegisterLegacyCommand s1("nix-channel", _main);
|