aboutsummaryrefslogtreecommitdiff
path: root/nix/libutil/types.hh
blob: 160884ee1ad7230b108d1b5fe664f736d1c95239 (about) (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
#pragma once

#include "config.h"

#include <string>
#include <list>
#include <set>

#include <boost/format.hpp>

/* Before 4.7, gcc's std::exception uses empty throw() specifiers for
 * its (virtual) destructor and what() in c++11 mode, in violation of spec
 */
#ifdef __GNUC__
#if __GNUC__ < 4 || (__GNUC__ == 4 && __GNUC_MINOR__ < 7)
#define EXCEPTION_NEEDS_THROW_SPEC
#endif
#endif


namespace nix {


/* Inherit some names from other namespaces for convenience. */
using std::string;
using std::list;
using std::set;
using std::vector;
using boost::format;


struct FormatOrString
{
    string s;
    FormatOrString(const string & s) : s(s) { };
    FormatOrString(const format & f) : s(f.str()) { };
    FormatOrString(const char * s) : s(s) { };
};


/* BaseError should generally not be caught, as it has Interrupted as
   a subclass. Catch Error instead. */
class BaseError : public std::exception
{
protected:
    string prefix_; // used for location traces etc.
    string err;
public:
    unsigned int status; // exit status
    BaseError(const FormatOrString & fs, unsigned int status = 1);
#ifdef EXCEPTION_NEEDS_THROW_SPEC
    ~BaseError() throw () { };
    const char * what() const throw () { return err.c_str(); }
#else
    const char * what() const noexcept { return err.c_str(); }
#endif
    const string & msg() const { return err; }
    const string & prefix() const { return prefix_; }
    BaseError & addPrefix(const FormatOrString & fs);
};

#define MakeError(newClass, superClass) \
    class newClass : public superClass                  \
    {                                                   \
    public:                                             \
        newClass(const FormatOrString & fs, unsigned int status = 1) : superClass(fs, status) { }; \
    };

MakeError(Error, BaseError)

class SysError : public Error
{
public:
    int errNo;
    SysError(const FormatOrString & fs);
};


typedef list<string> Strings;
typedef set<string> StringSet;


/* Paths are just strings. */
typedef string Path;
typedef list<Path> Paths;
typedef set<Path> PathSet;


typedef enum {
    lvlError = 0,
    lvlInfo,
    lvlTalkative,
    lvlChatty,
    lvlDebug,
    lvlVomit
} Verbosity;


}
ot; (readlink (string-append (getenv "HOME") "/.guix-profile")))))' cd "$tmpdir" \ && guix environment --bootstrap --container --link-profile \ --ad-hoc guile-bootstrap --pure \ -- guile -c "$linktest" ) # Test that user can be mocked. usertest='(exit (and (string=? (getenv "HOME") "/home/foognu") (string=? (passwd:name (getpwuid 1000)) "foognu") (file-exists? "/home/foognu/umock")))' touch "$tmpdir/umock" HOME="$tmpdir" guix environment --bootstrap --container --user=foognu \ --ad-hoc guile-bootstrap --pure \ --share="$tmpdir/umock" \ -- guile -c "$usertest" # if not sharing CWD, chdir home ( cd "$tmpdir" \ && guix environment --bootstrap --container --no-cwd --user=foo \ --ad-hoc guile-bootstrap --pure \ -- /bin/sh -c 'test $(pwd) == "/home/foo" -a ! -d '"$tmpdir" ) # Check the exit code. abnormal_exit_code=" (use-modules (system foreign)) ;; Purposely make Guile crash with a segfault. :) (pointer->string (make-pointer 123) 123)" if guix environment --bootstrap --container \ --ad-hoc guile-bootstrap -- guile -c "$abnormal_exit_code" then false; else test $? -gt 127 fi # Test the Filesystem Hierarchy Standard (FHS) container option, --emulate-fhs (-F) # As this option requires a glibc package (glibc-for-fhs), try to run these # tests with the user's global store to make it easier to build or download a # substitute. storedir="`guile -c '(use-modules (guix config))(display %storedir)'`" localstatedir="`guile -c '(use-modules (guix config))(display %localstatedir)'`" NIX_STORE_DIR="$storedir" GUIX_DAEMON_SOCKET="$localstatedir/guix/daemon-socket/socket" export NIX_STORE_DIR GUIX_DAEMON_SOCKET if ! guile -c '(use-modules (guix)) (exit (false-if-exception (open-connection)))' then exit 77 fi # Test that the container has FHS specific files/directories. Note that /bin # exists in a non-FHS container as it will contain sh, a symlink to the bash # package, so we don't test for it. guix shell -C --emulate-fhs --bootstrap guile-bootstrap \ -- guile -c '(exit (and (file-exists? "/etc/ld.so.cache") (file-exists? "/lib") (file-exists? "/sbin") (file-exists? "/usr/bin") (file-exists? "/usr/include") (file-exists? "/usr/lib") (file-exists? "/usr/libexec") (file-exists? "/usr/sbin") (file-exists? "/usr/share")))' # Test that the ld cache was generated and can be successfully read. guix shell -CF --bootstrap guile-bootstrap \ -- guile -c '(execlp "ldconfig" "ldconfig" "-p")' # Test that the package glibc-for-fhs is in the container even if there is the # regular glibc package from another source. See # <https://issues.guix.gnu.org/58861>. guix shell -CF --bootstrap guile-bootstrap glibc \ -- guile -c '(exit (if (string-contains (readlink "/lib/libc.so") "glibc-for-fhs") 0 1))' # Test that $PATH inside the container includes the FHS directories. guix shell -CF coreutils -- env | grep ^PATH=/bin:/usr/bin:/sbin:/usr/sbin.* # Make sure '--preserve' is honored for $PATH, which the '--emulate-fhs' # option modifies. We can't (easily) check the whole $PATH as it will differ # inside and outside the container, so just check our test $PATH is still # present. See <https://issues.guix.gnu.org/60566>. PATH=/foo $(type -P guix) shell -CF -E ^PATH$ coreutils \ -- env | grep ^PATH=.*:/foo # '--symlink' works. echo "TESTING SYMLINK IN CONTAINER" guix shell --bootstrap guile-bootstrap --container \ --symlink=/usr/bin/guile=bin/guile -- \ /usr/bin/guile --version # A dangling symlink causes the command to fail. guix shell --bootstrap -CS /usr/bin/python=bin/python guile-bootstrap -- exit && false # An invalid symlink spec causes the command to fail. guix shell --bootstrap -CS bin/guile=/usr/bin/guile guile-bootstrap -- exit && false # Check whether '--nesting' works. guix build hello -d env="$(type -P pre-inst-env)" guix shell -C -D guix -- "$env" guix build hello -d && false # cannot work hello_drv="$(guix build hello -d)" hello_drv_nested="$(cd "$(dirname env)" && guix shell --bootstrap -E GUIX_BUILD_OPTIONS -CW -D guix -- "$env" guix build hello -d)" test "$hello_drv" = "$hello_drv_nested"