2006-11-30 19:19:59 +00:00
|
|
|
#include "shared.hh"
|
|
|
|
#include "local-store.hh"
|
|
|
|
#include "util.hh"
|
2006-11-30 19:54:43 +00:00
|
|
|
#include "serialise.hh"
|
2006-11-30 20:45:20 +00:00
|
|
|
#include "archive.hh"
|
2006-12-03 16:25:19 +00:00
|
|
|
#include "globals.hh"
|
2015-09-03 10:56:59 +00:00
|
|
|
#include "derivations.hh"
|
2017-08-28 12:17:07 +00:00
|
|
|
#include "finally.hh"
|
2020-03-30 12:29:29 +00:00
|
|
|
#include "../nix/legacy.hh"
|
2018-09-24 11:53:44 +00:00
|
|
|
#include "daemon.hh"
|
2006-11-30 19:19:59 +00:00
|
|
|
|
2014-07-17 14:57:07 +00:00
|
|
|
#include <algorithm>
|
2019-11-06 15:53:02 +00:00
|
|
|
#include <climits>
|
2010-06-24 17:51:04 +00:00
|
|
|
#include <cstring>
|
2019-11-06 15:53:02 +00:00
|
|
|
|
2006-12-03 03:03:36 +00:00
|
|
|
#include <unistd.h>
|
|
|
|
#include <signal.h>
|
2006-12-04 17:17:13 +00:00
|
|
|
#include <sys/types.h>
|
2006-12-05 17:21:42 +00:00
|
|
|
#include <sys/wait.h>
|
2006-12-04 17:17:13 +00:00
|
|
|
#include <sys/stat.h>
|
|
|
|
#include <sys/socket.h>
|
|
|
|
#include <sys/un.h>
|
2006-12-05 17:21:42 +00:00
|
|
|
#include <errno.h>
|
2014-07-17 13:49:33 +00:00
|
|
|
#include <pwd.h>
|
2014-07-17 14:57:07 +00:00
|
|
|
#include <grp.h>
|
2016-09-02 17:30:28 +00:00
|
|
|
#include <fcntl.h>
|
2006-12-02 14:27:24 +00:00
|
|
|
|
2014-10-31 09:08:59 +00:00
|
|
|
#if __APPLE__ || __FreeBSD__
|
|
|
|
#include <sys/ucred.h>
|
|
|
|
#endif
|
|
|
|
|
2006-11-30 19:19:59 +00:00
|
|
|
using namespace nix;
|
2018-09-24 11:53:44 +00:00
|
|
|
using namespace nix::daemon;
|
2006-11-30 19:19:59 +00:00
|
|
|
|
2016-09-02 17:30:28 +00:00
|
|
|
#ifndef __linux__
|
|
|
|
#define SPLICE_F_MOVE 0
|
2016-11-17 13:10:12 +00:00
|
|
|
static ssize_t splice(int fd_in, void *off_in, int fd_out, void *off_out, size_t len, unsigned int flags)
|
2016-09-02 17:30:28 +00:00
|
|
|
{
|
|
|
|
/* We ignore most parameters, we just have them for conformance with the linux syscall */
|
2018-03-01 21:00:58 +00:00
|
|
|
std::vector<char> buf(8192);
|
|
|
|
auto read_count = read(fd_in, buf.data(), buf.size());
|
2016-09-02 17:30:28 +00:00
|
|
|
if (read_count == -1)
|
|
|
|
return read_count;
|
2016-11-17 13:10:12 +00:00
|
|
|
auto write_count = decltype(read_count)(0);
|
2016-09-02 17:30:28 +00:00
|
|
|
while (write_count < read_count) {
|
2018-03-01 21:00:58 +00:00
|
|
|
auto res = write(fd_out, buf.data() + write_count, read_count - write_count);
|
2016-09-02 17:30:28 +00:00
|
|
|
if (res == -1)
|
|
|
|
return res;
|
|
|
|
write_count += res;
|
|
|
|
}
|
|
|
|
return read_count;
|
|
|
|
}
|
|
|
|
#endif
|
2006-11-30 19:19:59 +00:00
|
|
|
|
|
|
|
|
2006-12-05 17:21:42 +00:00
|
|
|
static void sigChldHandler(int sigNo)
|
|
|
|
{
|
2018-03-15 02:39:01 +00:00
|
|
|
// Ensure we don't modify errno of whatever we've interrupted
|
|
|
|
auto saved_errno = errno;
|
2006-12-05 17:21:42 +00:00
|
|
|
/* Reap all dead children. */
|
2008-11-14 16:50:01 +00:00
|
|
|
while (waitpid(-1, 0, WNOHANG) > 0) ;
|
2018-03-15 02:39:01 +00:00
|
|
|
errno = saved_errno;
|
2006-12-05 17:21:42 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
static void setSigChldAction(bool autoReap)
|
2006-12-04 17:17:13 +00:00
|
|
|
{
|
|
|
|
struct sigaction act, oact;
|
2006-12-05 17:21:42 +00:00
|
|
|
act.sa_handler = autoReap ? sigChldHandler : SIG_DFL;
|
2006-12-04 17:17:13 +00:00
|
|
|
sigfillset(&act.sa_mask);
|
|
|
|
act.sa_flags = 0;
|
|
|
|
if (sigaction(SIGCHLD, &act, &oact))
|
|
|
|
throw SysError("setting SIGCHLD handler");
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2014-07-17 14:57:07 +00:00
|
|
|
bool matchUser(const string & user, const string & group, const Strings & users)
|
|
|
|
{
|
|
|
|
if (find(users.begin(), users.end(), "*") != users.end())
|
|
|
|
return true;
|
|
|
|
|
|
|
|
if (find(users.begin(), users.end(), user) != users.end())
|
|
|
|
return true;
|
|
|
|
|
|
|
|
for (auto & i : users)
|
|
|
|
if (string(i, 0, 1) == "@") {
|
|
|
|
if (group == string(i, 1)) return true;
|
|
|
|
struct group * gr = getgrnam(i.c_str() + 1);
|
|
|
|
if (!gr) continue;
|
|
|
|
for (char * * mem = gr->gr_mem; *mem; mem++)
|
|
|
|
if (user == string(*mem)) return true;
|
|
|
|
}
|
|
|
|
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2014-10-31 09:08:59 +00:00
|
|
|
struct PeerInfo
|
|
|
|
{
|
|
|
|
bool pidKnown;
|
|
|
|
pid_t pid;
|
|
|
|
bool uidKnown;
|
|
|
|
uid_t uid;
|
|
|
|
bool gidKnown;
|
|
|
|
gid_t gid;
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
|
|
/* Get the identity of the caller, if possible. */
|
|
|
|
static PeerInfo getPeerInfo(int remote)
|
|
|
|
{
|
|
|
|
PeerInfo peer = { false, 0, false, 0, false, 0 };
|
|
|
|
|
|
|
|
#if defined(SO_PEERCRED)
|
|
|
|
|
|
|
|
ucred cred;
|
|
|
|
socklen_t credLen = sizeof(cred);
|
|
|
|
if (getsockopt(remote, SOL_SOCKET, SO_PEERCRED, &cred, &credLen) == -1)
|
|
|
|
throw SysError("getting peer credentials");
|
|
|
|
peer = { true, cred.pid, true, cred.uid, true, cred.gid };
|
|
|
|
|
|
|
|
#elif defined(LOCAL_PEERCRED)
|
|
|
|
|
2015-10-04 11:53:23 +00:00
|
|
|
#if !defined(SOL_LOCAL)
|
|
|
|
#define SOL_LOCAL 0
|
|
|
|
#endif
|
|
|
|
|
2014-10-31 09:08:59 +00:00
|
|
|
xucred cred;
|
|
|
|
socklen_t credLen = sizeof(cred);
|
|
|
|
if (getsockopt(remote, SOL_LOCAL, LOCAL_PEERCRED, &cred, &credLen) == -1)
|
|
|
|
throw SysError("getting peer credentials");
|
|
|
|
peer = { false, 0, true, cred.cr_uid, false, 0 };
|
|
|
|
|
|
|
|
#endif
|
|
|
|
|
|
|
|
return peer;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2012-06-19 03:01:46 +00:00
|
|
|
#define SD_LISTEN_FDS_START 3
|
|
|
|
|
|
|
|
|
2018-09-25 10:49:20 +00:00
|
|
|
static ref<Store> openUncachedStore()
|
|
|
|
{
|
|
|
|
Store::Params params; // FIXME: get params from somewhere
|
|
|
|
// Disable caching since the client already does that.
|
|
|
|
params["path-info-cache-size"] = "0";
|
|
|
|
return openStore(settings.storeUri, params);
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2014-08-13 01:50:44 +00:00
|
|
|
static void daemonLoop(char * * argv)
|
2006-12-04 17:17:13 +00:00
|
|
|
{
|
2014-12-12 16:14:28 +00:00
|
|
|
if (chdir("/") == -1)
|
|
|
|
throw SysError("cannot change current directory");
|
2014-08-13 01:50:44 +00:00
|
|
|
|
2006-12-04 17:17:13 +00:00
|
|
|
/* Get rid of children automatically; don't let them become
|
|
|
|
zombies. */
|
|
|
|
setSigChldAction(true);
|
|
|
|
|
2012-06-19 03:01:46 +00:00
|
|
|
AutoCloseFD fdSocket;
|
|
|
|
|
|
|
|
/* Handle socket-based activation by systemd. */
|
2019-11-22 15:06:44 +00:00
|
|
|
auto listenFds = getEnv("LISTEN_FDS");
|
|
|
|
if (listenFds) {
|
|
|
|
if (getEnv("LISTEN_PID") != std::to_string(getpid()) || listenFds != "1")
|
2012-06-19 03:01:46 +00:00
|
|
|
throw Error("unexpected systemd environment variables");
|
|
|
|
fdSocket = SD_LISTEN_FDS_START;
|
2018-09-25 10:36:11 +00:00
|
|
|
closeOnExec(fdSocket.get());
|
2012-06-19 03:01:46 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
/* Otherwise, create and bind to a Unix domain socket. */
|
|
|
|
else {
|
2018-09-25 10:36:11 +00:00
|
|
|
createDirs(dirOf(settings.nixDaemonSocketFile));
|
|
|
|
fdSocket = createUnixDomainSocket(settings.nixDaemonSocketFile, 0666);
|
2012-06-19 03:01:46 +00:00
|
|
|
}
|
2006-12-04 17:17:13 +00:00
|
|
|
|
|
|
|
/* Loop accepting connections. */
|
|
|
|
while (1) {
|
|
|
|
|
|
|
|
try {
|
|
|
|
/* Accept a connection. */
|
|
|
|
struct sockaddr_un remoteAddr;
|
|
|
|
socklen_t remoteAddrLen = sizeof(remoteAddr);
|
|
|
|
|
2016-07-11 19:44:44 +00:00
|
|
|
AutoCloseFD remote = accept(fdSocket.get(),
|
2006-12-04 17:17:13 +00:00
|
|
|
(struct sockaddr *) &remoteAddr, &remoteAddrLen);
|
|
|
|
checkInterrupt();
|
2016-07-11 19:44:44 +00:00
|
|
|
if (!remote) {
|
2014-07-23 17:21:00 +00:00
|
|
|
if (errno == EINTR) continue;
|
|
|
|
throw SysError("accepting connection");
|
2009-09-30 11:32:04 +00:00
|
|
|
}
|
2006-12-04 17:17:13 +00:00
|
|
|
|
2016-07-11 19:44:44 +00:00
|
|
|
closeOnExec(remote.get());
|
2012-07-30 21:13:25 +00:00
|
|
|
|
Recursive Nix support
This allows Nix builders to call Nix to build derivations, with some
limitations.
Example:
let nixpkgs = fetchTarball channel:nixos-18.03; in
with import <nixpkgs> {};
runCommand "foo"
{
buildInputs = [ nix jq ];
NIX_PATH = "nixpkgs=${nixpkgs}";
}
''
hello=$(nix-build -E '(import <nixpkgs> {}).hello.overrideDerivation (args: { name = "hello-3.5"; })')
$hello/bin/hello
mkdir -p $out/bin
ln -s $hello/bin/hello $out/bin/hello
nix path-info -r --json $hello | jq .
''
This derivation makes a recursive Nix call to build GNU Hello and
symlinks it from its $out, i.e.
# ll ./result/bin/
lrwxrwxrwx 1 root root 63 Jan 1 1970 hello -> /nix/store/s0awxrs71gickhaqdwxl506hzccb30y5-hello-3.5/bin/hello
# nix-store -qR ./result
/nix/store/hwwqshlmazzjzj7yhrkyjydxamvvkfd3-glibc-2.26-131
/nix/store/s0awxrs71gickhaqdwxl506hzccb30y5-hello-3.5
/nix/store/sgmvvyw8vhfqdqb619bxkcpfn9lvd8ss-foo
This is implemented as follows:
* Before running the outer builder, Nix creates a Unix domain socket
'.nix-socket' in the builder's temporary directory and sets
$NIX_REMOTE to point to it. It starts a thread to process
connections to this socket. (Thus you don't need to have nix-daemon
running.)
* The daemon thread uses a wrapper store (RestrictedStore) to keep
track of paths added through recursive Nix calls, to implement some
restrictions (see below), and to do some censorship (e.g. for
purity, queryPathInfo() won't return impure information such as
signatures and timestamps).
* After the build finishes, the output paths are scanned for
references to the paths added through recursive Nix calls (in
addition to the inputs closure). Thus, in the example above, $out
has a reference to $hello.
The main restriction on recursive Nix calls is that they cannot do
arbitrary substitutions. For example, doing
nix-store -r /nix/store/kmwd1hq55akdb9sc7l3finr175dajlby-hello-2.10
is forbidden unless /nix/store/kmwd... is in the inputs closure or
previously built by a recursive Nix call. This is to prevent
irreproducible derivations that have hidden dependencies on
substituters or the current store contents. Building a derivation is
fine, however, and Nix will use substitutes if available. In other
words, the builder has to present proof that it knows how to build a
desired store path from scratch by constructing a derivation graph for
that path.
Probably we should also disallow instantiating/building fixed-output
derivations (specifically, those that access the network, but
currently we have no way to mark fixed-output derivations that don't
access the network). Otherwise sandboxed derivations can bypass
sandbox restrictions and access the network.
When sandboxing is enabled, we make paths appear in the sandbox of the
builder by entering the mount namespace of the builder and
bind-mounting each path. This is tricky because we do a pivot_root()
in the builder to change the root directory of its mount namespace,
and thus the host /nix/store is not visible in the mount namespace of
the builder. To get around this, just before doing pivot_root(), we
branch a second mount namespace that shares its /nix/store mountpoint
with the parent.
Recursive Nix currently doesn't work on macOS in sandboxed mode
(because we can't change the sandbox policy of a running build) and in
non-root mode (because setns() barfs).
2018-10-02 14:01:26 +00:00
|
|
|
TrustedFlag trusted = NotTrusted;
|
2016-07-11 19:44:44 +00:00
|
|
|
PeerInfo peer = getPeerInfo(remote.get());
|
2014-07-17 13:41:11 +00:00
|
|
|
|
2014-10-31 09:08:59 +00:00
|
|
|
struct passwd * pw = peer.uidKnown ? getpwuid(peer.uid) : 0;
|
2015-10-29 12:26:55 +00:00
|
|
|
string user = pw ? pw->pw_name : std::to_string(peer.uid);
|
2006-12-04 17:17:13 +00:00
|
|
|
|
2014-10-31 09:08:59 +00:00
|
|
|
struct group * gr = peer.gidKnown ? getgrgid(peer.gid) : 0;
|
2015-10-29 12:26:55 +00:00
|
|
|
string group = gr ? gr->gr_name : std::to_string(peer.gid);
|
2014-07-17 14:57:07 +00:00
|
|
|
|
Explicitly model all settings and fail on unrecognized ones
Previously, the Settings class allowed other code to query for string
properties, which led to a proliferation of code all over the place making
up new options without any sort of central registry of valid options. This
commit pulls all those options back into the central Settings class and
removes the public get() methods, to discourage future abuses like that.
Furthermore, because we know the full set of options ahead of time, we
now fail loudly if someone enters an unrecognized option, thus preventing
subtle typos. With some template fun, we could probably also dump the full
set of options (with documentation, defaults, etc.) to the command line,
but I'm not doing that yet here.
2017-02-22 03:50:18 +00:00
|
|
|
Strings trustedUsers = settings.trustedUsers;
|
|
|
|
Strings allowedUsers = settings.allowedUsers;
|
2014-08-04 16:13:14 +00:00
|
|
|
|
2014-08-05 08:19:57 +00:00
|
|
|
if (matchUser(user, group, trustedUsers))
|
Recursive Nix support
This allows Nix builders to call Nix to build derivations, with some
limitations.
Example:
let nixpkgs = fetchTarball channel:nixos-18.03; in
with import <nixpkgs> {};
runCommand "foo"
{
buildInputs = [ nix jq ];
NIX_PATH = "nixpkgs=${nixpkgs}";
}
''
hello=$(nix-build -E '(import <nixpkgs> {}).hello.overrideDerivation (args: { name = "hello-3.5"; })')
$hello/bin/hello
mkdir -p $out/bin
ln -s $hello/bin/hello $out/bin/hello
nix path-info -r --json $hello | jq .
''
This derivation makes a recursive Nix call to build GNU Hello and
symlinks it from its $out, i.e.
# ll ./result/bin/
lrwxrwxrwx 1 root root 63 Jan 1 1970 hello -> /nix/store/s0awxrs71gickhaqdwxl506hzccb30y5-hello-3.5/bin/hello
# nix-store -qR ./result
/nix/store/hwwqshlmazzjzj7yhrkyjydxamvvkfd3-glibc-2.26-131
/nix/store/s0awxrs71gickhaqdwxl506hzccb30y5-hello-3.5
/nix/store/sgmvvyw8vhfqdqb619bxkcpfn9lvd8ss-foo
This is implemented as follows:
* Before running the outer builder, Nix creates a Unix domain socket
'.nix-socket' in the builder's temporary directory and sets
$NIX_REMOTE to point to it. It starts a thread to process
connections to this socket. (Thus you don't need to have nix-daemon
running.)
* The daemon thread uses a wrapper store (RestrictedStore) to keep
track of paths added through recursive Nix calls, to implement some
restrictions (see below), and to do some censorship (e.g. for
purity, queryPathInfo() won't return impure information such as
signatures and timestamps).
* After the build finishes, the output paths are scanned for
references to the paths added through recursive Nix calls (in
addition to the inputs closure). Thus, in the example above, $out
has a reference to $hello.
The main restriction on recursive Nix calls is that they cannot do
arbitrary substitutions. For example, doing
nix-store -r /nix/store/kmwd1hq55akdb9sc7l3finr175dajlby-hello-2.10
is forbidden unless /nix/store/kmwd... is in the inputs closure or
previously built by a recursive Nix call. This is to prevent
irreproducible derivations that have hidden dependencies on
substituters or the current store contents. Building a derivation is
fine, however, and Nix will use substitutes if available. In other
words, the builder has to present proof that it knows how to build a
desired store path from scratch by constructing a derivation graph for
that path.
Probably we should also disallow instantiating/building fixed-output
derivations (specifically, those that access the network, but
currently we have no way to mark fixed-output derivations that don't
access the network). Otherwise sandboxed derivations can bypass
sandbox restrictions and access the network.
When sandboxing is enabled, we make paths appear in the sandbox of the
builder by entering the mount namespace of the builder and
bind-mounting each path. This is tricky because we do a pivot_root()
in the builder to change the root directory of its mount namespace,
and thus the host /nix/store is not visible in the mount namespace of
the builder. To get around this, just before doing pivot_root(), we
branch a second mount namespace that shares its /nix/store mountpoint
with the parent.
Recursive Nix currently doesn't work on macOS in sandboxed mode
(because we can't change the sandbox policy of a running build) and in
non-root mode (because setns() barfs).
2018-10-02 14:01:26 +00:00
|
|
|
trusted = Trusted;
|
2014-07-17 14:57:07 +00:00
|
|
|
|
2018-02-07 21:17:44 +00:00
|
|
|
if ((!trusted && !matchUser(user, group, allowedUsers)) || group == settings.buildUsersGroup)
|
2017-07-30 11:27:57 +00:00
|
|
|
throw Error(format("user '%1%' is not allowed to connect to the Nix daemon") % user);
|
2014-07-17 13:49:33 +00:00
|
|
|
|
2016-09-21 14:11:01 +00:00
|
|
|
printInfo(format((string) "accepted connection from pid %1%, user %2%" + (trusted ? " (trusted)" : ""))
|
2015-10-29 12:26:55 +00:00
|
|
|
% (peer.pidKnown ? std::to_string(peer.pid) : "<unknown>")
|
2014-10-31 09:08:59 +00:00
|
|
|
% (peer.uidKnown ? user : "<unknown>"));
|
2012-07-30 21:13:25 +00:00
|
|
|
|
2006-12-04 17:17:13 +00:00
|
|
|
/* Fork a child to handle the connection. */
|
2014-12-10 15:35:42 +00:00
|
|
|
ProcessOptions options;
|
|
|
|
options.errorPrefix = "unexpected Nix daemon error: ";
|
|
|
|
options.dieWithParent = false;
|
|
|
|
options.runExitHandlers = true;
|
|
|
|
options.allowVfork = false;
|
2014-07-10 14:50:51 +00:00
|
|
|
startProcess([&]() {
|
2016-07-11 19:44:44 +00:00
|
|
|
fdSocket = -1;
|
2014-09-25 16:45:43 +00:00
|
|
|
|
2014-07-10 14:50:51 +00:00
|
|
|
/* Background the daemon. */
|
|
|
|
if (setsid() == -1)
|
|
|
|
throw SysError(format("creating a new session"));
|
|
|
|
|
|
|
|
/* Restore normal handling of SIGCHLD. */
|
|
|
|
setSigChldAction(false);
|
|
|
|
|
|
|
|
/* For debugging, stuff the pid into argv[1]. */
|
2014-10-31 09:08:59 +00:00
|
|
|
if (peer.pidKnown && argv[1]) {
|
2015-10-29 12:26:55 +00:00
|
|
|
string processName = std::to_string(peer.pid);
|
2014-08-13 01:50:44 +00:00
|
|
|
strncpy(argv[1], processName.c_str(), strlen(argv[1]));
|
2014-07-10 14:50:51 +00:00
|
|
|
}
|
2012-07-30 21:13:25 +00:00
|
|
|
|
2014-07-10 14:50:51 +00:00
|
|
|
/* Handle the connection. */
|
2018-09-24 11:53:44 +00:00
|
|
|
FdSource from(remote.get());
|
|
|
|
FdSink to(remote.get());
|
Recursive Nix support
This allows Nix builders to call Nix to build derivations, with some
limitations.
Example:
let nixpkgs = fetchTarball channel:nixos-18.03; in
with import <nixpkgs> {};
runCommand "foo"
{
buildInputs = [ nix jq ];
NIX_PATH = "nixpkgs=${nixpkgs}";
}
''
hello=$(nix-build -E '(import <nixpkgs> {}).hello.overrideDerivation (args: { name = "hello-3.5"; })')
$hello/bin/hello
mkdir -p $out/bin
ln -s $hello/bin/hello $out/bin/hello
nix path-info -r --json $hello | jq .
''
This derivation makes a recursive Nix call to build GNU Hello and
symlinks it from its $out, i.e.
# ll ./result/bin/
lrwxrwxrwx 1 root root 63 Jan 1 1970 hello -> /nix/store/s0awxrs71gickhaqdwxl506hzccb30y5-hello-3.5/bin/hello
# nix-store -qR ./result
/nix/store/hwwqshlmazzjzj7yhrkyjydxamvvkfd3-glibc-2.26-131
/nix/store/s0awxrs71gickhaqdwxl506hzccb30y5-hello-3.5
/nix/store/sgmvvyw8vhfqdqb619bxkcpfn9lvd8ss-foo
This is implemented as follows:
* Before running the outer builder, Nix creates a Unix domain socket
'.nix-socket' in the builder's temporary directory and sets
$NIX_REMOTE to point to it. It starts a thread to process
connections to this socket. (Thus you don't need to have nix-daemon
running.)
* The daemon thread uses a wrapper store (RestrictedStore) to keep
track of paths added through recursive Nix calls, to implement some
restrictions (see below), and to do some censorship (e.g. for
purity, queryPathInfo() won't return impure information such as
signatures and timestamps).
* After the build finishes, the output paths are scanned for
references to the paths added through recursive Nix calls (in
addition to the inputs closure). Thus, in the example above, $out
has a reference to $hello.
The main restriction on recursive Nix calls is that they cannot do
arbitrary substitutions. For example, doing
nix-store -r /nix/store/kmwd1hq55akdb9sc7l3finr175dajlby-hello-2.10
is forbidden unless /nix/store/kmwd... is in the inputs closure or
previously built by a recursive Nix call. This is to prevent
irreproducible derivations that have hidden dependencies on
substituters or the current store contents. Building a derivation is
fine, however, and Nix will use substitutes if available. In other
words, the builder has to present proof that it knows how to build a
desired store path from scratch by constructing a derivation graph for
that path.
Probably we should also disallow instantiating/building fixed-output
derivations (specifically, those that access the network, but
currently we have no way to mark fixed-output derivations that don't
access the network). Otherwise sandboxed derivations can bypass
sandbox restrictions and access the network.
When sandboxing is enabled, we make paths appear in the sandbox of the
builder by entering the mount namespace of the builder and
bind-mounting each path. This is tricky because we do a pivot_root()
in the builder to change the root directory of its mount namespace,
and thus the host /nix/store is not visible in the mount namespace of
the builder. To get around this, just before doing pivot_root(), we
branch a second mount namespace that shares its /nix/store mountpoint
with the parent.
Recursive Nix currently doesn't work on macOS in sandboxed mode
(because we can't change the sandbox policy of a running build) and in
non-root mode (because setns() barfs).
2018-10-02 14:01:26 +00:00
|
|
|
processConnection(openUncachedStore(), from, to, trusted, NotRecursive, user, peer.uid);
|
2012-07-30 21:13:25 +00:00
|
|
|
|
2014-11-19 16:09:27 +00:00
|
|
|
exit(0);
|
2014-12-10 15:35:42 +00:00
|
|
|
}, options);
|
2006-12-04 17:17:13 +00:00
|
|
|
|
|
|
|
} catch (Interrupted & e) {
|
2018-04-09 12:05:54 +00:00
|
|
|
return;
|
2006-12-04 17:17:13 +00:00
|
|
|
} catch (Error & e) {
|
2016-09-21 14:11:01 +00:00
|
|
|
printError(format("error processing connection: %1%") % e.msg());
|
2006-12-04 17:17:13 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2018-10-26 09:35:46 +00:00
|
|
|
static int _main(int argc, char * * argv)
|
2006-11-30 19:19:59 +00:00
|
|
|
{
|
2018-10-26 09:35:46 +00:00
|
|
|
{
|
2016-09-02 17:30:28 +00:00
|
|
|
auto stdio = false;
|
|
|
|
|
2014-08-13 01:50:44 +00:00
|
|
|
parseCmdLine(argc, argv, [&](Strings::iterator & arg, const Strings::iterator & end) {
|
|
|
|
if (*arg == "--daemon")
|
|
|
|
; /* ignored for backwards compatibility */
|
|
|
|
else if (*arg == "--help")
|
|
|
|
showManPage("nix-daemon");
|
|
|
|
else if (*arg == "--version")
|
|
|
|
printVersion("nix-daemon");
|
2016-09-02 17:30:28 +00:00
|
|
|
else if (*arg == "--stdio")
|
|
|
|
stdio = true;
|
2014-08-13 01:50:44 +00:00
|
|
|
else return false;
|
|
|
|
return true;
|
|
|
|
});
|
|
|
|
|
2018-02-08 16:26:18 +00:00
|
|
|
initPlugins();
|
|
|
|
|
2016-09-02 17:30:28 +00:00
|
|
|
if (stdio) {
|
|
|
|
if (getStoreType() == tDaemon) {
|
|
|
|
/* Forward on this connection to the real daemon */
|
|
|
|
auto socketPath = settings.nixDaemonSocketFile;
|
|
|
|
auto s = socket(PF_UNIX, SOCK_STREAM, 0);
|
|
|
|
if (s == -1)
|
|
|
|
throw SysError("creating Unix domain socket");
|
|
|
|
|
|
|
|
auto socketDir = dirOf(socketPath);
|
|
|
|
if (chdir(socketDir.c_str()) == -1)
|
2017-07-30 11:27:57 +00:00
|
|
|
throw SysError(format("changing to socket directory '%1%'") % socketDir);
|
2016-09-02 17:30:28 +00:00
|
|
|
|
2019-12-05 18:11:09 +00:00
|
|
|
auto socketName = std::string(baseNameOf(socketPath));
|
2016-09-02 17:30:28 +00:00
|
|
|
auto addr = sockaddr_un{};
|
|
|
|
addr.sun_family = AF_UNIX;
|
|
|
|
if (socketName.size() + 1 >= sizeof(addr.sun_path))
|
|
|
|
throw Error(format("socket name %1% is too long") % socketName);
|
|
|
|
strcpy(addr.sun_path, socketName.c_str());
|
|
|
|
|
|
|
|
if (connect(s, (struct sockaddr *) &addr, sizeof(addr)) == -1)
|
|
|
|
throw SysError(format("cannot connect to daemon at %1%") % socketPath);
|
|
|
|
|
|
|
|
auto nfds = (s > STDIN_FILENO ? s : STDIN_FILENO) + 1;
|
|
|
|
while (true) {
|
|
|
|
fd_set fds;
|
|
|
|
FD_ZERO(&fds);
|
|
|
|
FD_SET(s, &fds);
|
|
|
|
FD_SET(STDIN_FILENO, &fds);
|
|
|
|
if (select(nfds, &fds, nullptr, nullptr, nullptr) == -1)
|
|
|
|
throw SysError("waiting for data from client or server");
|
|
|
|
if (FD_ISSET(s, &fds)) {
|
2017-02-13 13:06:46 +00:00
|
|
|
auto res = splice(s, nullptr, STDOUT_FILENO, nullptr, SSIZE_MAX, SPLICE_F_MOVE);
|
2016-09-02 17:30:28 +00:00
|
|
|
if (res == -1)
|
|
|
|
throw SysError("splicing data from daemon socket to stdout");
|
|
|
|
else if (res == 0)
|
|
|
|
throw EndOfFile("unexpected EOF from daemon socket");
|
|
|
|
}
|
|
|
|
if (FD_ISSET(STDIN_FILENO, &fds)) {
|
2017-02-13 13:06:46 +00:00
|
|
|
auto res = splice(STDIN_FILENO, nullptr, s, nullptr, SSIZE_MAX, SPLICE_F_MOVE);
|
2016-09-02 17:30:28 +00:00
|
|
|
if (res == -1)
|
|
|
|
throw SysError("splicing data from stdin to daemon socket");
|
|
|
|
else if (res == 0)
|
2018-10-26 09:35:46 +00:00
|
|
|
return 0;
|
2016-09-02 17:30:28 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
} else {
|
2018-09-24 11:53:44 +00:00
|
|
|
FdSource from(STDIN_FILENO);
|
|
|
|
FdSink to(STDOUT_FILENO);
|
Recursive Nix support
This allows Nix builders to call Nix to build derivations, with some
limitations.
Example:
let nixpkgs = fetchTarball channel:nixos-18.03; in
with import <nixpkgs> {};
runCommand "foo"
{
buildInputs = [ nix jq ];
NIX_PATH = "nixpkgs=${nixpkgs}";
}
''
hello=$(nix-build -E '(import <nixpkgs> {}).hello.overrideDerivation (args: { name = "hello-3.5"; })')
$hello/bin/hello
mkdir -p $out/bin
ln -s $hello/bin/hello $out/bin/hello
nix path-info -r --json $hello | jq .
''
This derivation makes a recursive Nix call to build GNU Hello and
symlinks it from its $out, i.e.
# ll ./result/bin/
lrwxrwxrwx 1 root root 63 Jan 1 1970 hello -> /nix/store/s0awxrs71gickhaqdwxl506hzccb30y5-hello-3.5/bin/hello
# nix-store -qR ./result
/nix/store/hwwqshlmazzjzj7yhrkyjydxamvvkfd3-glibc-2.26-131
/nix/store/s0awxrs71gickhaqdwxl506hzccb30y5-hello-3.5
/nix/store/sgmvvyw8vhfqdqb619bxkcpfn9lvd8ss-foo
This is implemented as follows:
* Before running the outer builder, Nix creates a Unix domain socket
'.nix-socket' in the builder's temporary directory and sets
$NIX_REMOTE to point to it. It starts a thread to process
connections to this socket. (Thus you don't need to have nix-daemon
running.)
* The daemon thread uses a wrapper store (RestrictedStore) to keep
track of paths added through recursive Nix calls, to implement some
restrictions (see below), and to do some censorship (e.g. for
purity, queryPathInfo() won't return impure information such as
signatures and timestamps).
* After the build finishes, the output paths are scanned for
references to the paths added through recursive Nix calls (in
addition to the inputs closure). Thus, in the example above, $out
has a reference to $hello.
The main restriction on recursive Nix calls is that they cannot do
arbitrary substitutions. For example, doing
nix-store -r /nix/store/kmwd1hq55akdb9sc7l3finr175dajlby-hello-2.10
is forbidden unless /nix/store/kmwd... is in the inputs closure or
previously built by a recursive Nix call. This is to prevent
irreproducible derivations that have hidden dependencies on
substituters or the current store contents. Building a derivation is
fine, however, and Nix will use substitutes if available. In other
words, the builder has to present proof that it knows how to build a
desired store path from scratch by constructing a derivation graph for
that path.
Probably we should also disallow instantiating/building fixed-output
derivations (specifically, those that access the network, but
currently we have no way to mark fixed-output derivations that don't
access the network). Otherwise sandboxed derivations can bypass
sandbox restrictions and access the network.
When sandboxing is enabled, we make paths appear in the sandbox of the
builder by entering the mount namespace of the builder and
bind-mounting each path. This is tricky because we do a pivot_root()
in the builder to change the root directory of its mount namespace,
and thus the host /nix/store is not visible in the mount namespace of
the builder. To get around this, just before doing pivot_root(), we
branch a second mount namespace that shares its /nix/store mountpoint
with the parent.
Recursive Nix currently doesn't work on macOS in sandboxed mode
(because we can't change the sandbox policy of a running build) and in
non-root mode (because setns() barfs).
2018-10-02 14:01:26 +00:00
|
|
|
processConnection(openUncachedStore(), from, to, Trusted, NotRecursive, "root", 0);
|
2016-09-02 17:30:28 +00:00
|
|
|
}
|
|
|
|
} else {
|
|
|
|
daemonLoop(argv);
|
|
|
|
}
|
2018-10-26 09:35:46 +00:00
|
|
|
|
|
|
|
return 0;
|
|
|
|
}
|
2006-11-30 19:19:59 +00:00
|
|
|
}
|
2018-10-26 09:35:46 +00:00
|
|
|
|
|
|
|
static RegisterLegacyCommand s1("nix-daemon", _main);
|