about summary refs log tree commit diff
path: root/src/nix/upgrade-nix.cc
blob: 21892c31a89358a3e8918a0d58e7808daf1816b3 (plain) (blame)
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
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
#include "command.hh"
#include "store-api.hh"
#include "download.hh"
#include "eval.hh"
#include "attr-path.hh"

using namespace nix;

struct CmdUpgradeNix : StoreCommand
{
    Path profileDir;

    CmdUpgradeNix()
    {
        mkFlag()
            .longName("profile")
            .shortName('p')
            .labels({"profile-dir"})
            .description("the Nix profile to upgrade")
            .dest(&profileDir);
    }

    std::string name() override
    {
        return "upgrade-nix";
    }

    std::string description() override
    {
        return "upgrade Nix to the latest stable version";
    }

    Examples examples() override
    {
        return {
            Example{
                "To upgrade Nix to the latest stable version:",
                "nix upgrade-nix"
            },
            Example{
                "To upgrade Nix in a specific profile:",
                "nix upgrade-nix -p /nix/var/nix/profiles/per-user/alice/profile"
            },
        };
    }

    void run(ref<Store> store) override
    {
        evalSettings.pureEval = true;

        if (profileDir == "")
            profileDir = getProfileDir(store);

        printInfo("upgrading Nix in profile '%s'", profileDir);

        Path storePath;
        {
            Activity act(*logger, lvlInfo, actUnknown, "querying latest Nix version");
            storePath = getLatestNix(store);
        }

        {
            Activity act(*logger, lvlInfo, actUnknown, fmt("downloading '%s'...", storePath));
            store->ensurePath(storePath);
        }

        {
            Activity act(*logger, lvlInfo, actUnknown, fmt("verifying that '%s' works...", storePath));
            auto program = storePath + "/bin/nix-env";
            auto s = runProgram(program, false, {"--version"});
            if (s.find("Nix") == std::string::npos)
                throw Error("could not verify that '%s' works", program);
        }

        {
            Activity act(*logger, lvlInfo, actUnknown, fmt("installing '%s' into profile '%s'...", storePath, profileDir));
            runProgram(settings.nixBinDir + "/nix-env", false,
                {"--profile", profileDir, "-i", storePath, "--no-sandbox"});
        }
    }

    /* Return the profile in which Nix is installed. */
    Path getProfileDir(ref<Store> store)
    {
        Path where;

        for (auto & dir : tokenizeString<Strings>(getEnv("PATH"), ":"))
            if (pathExists(dir + "/nix-env")) {
                where = dir;
                break;
            }

        if (where == "")
            throw Error("couldn't figure out how Nix is installed, so I can't upgrade it");

        printInfo("found Nix in '%s'", where);

        if (hasPrefix(where, "/run/current-system"))
            throw Error("Nix on NixOS must be upgraded via 'nixos-rebuild'");

        Path profileDir;
        Path userEnv;

        if (baseNameOf(where) != "bin" ||
            !hasSuffix(userEnv = canonPath(profileDir = dirOf(where), true), "user-environment"))
            throw Error("directory '%s' does not appear to be part of a Nix profile", where);

        if (!store->isValidPath(userEnv))
            throw Error("directory '%s' is not in the Nix store", userEnv);

        return profileDir;
    }

    /* Return the store path of the latest stable Nix. */
    Path getLatestNix(ref<Store> store)
    {
        // FIXME: use nixos.org?
        auto req = DownloadRequest("https://github.com/NixOS/nixpkgs/raw/master/nixos/modules/installer/tools/nix-fallback-paths.nix");
        auto res = getDownloader()->download(req);

        EvalState state(Strings(), store);
        auto v = state.allocValue();
        state.eval(state.parseExprFromString(*res.data, "/no-such-path"), *v);
        Bindings & bindings(*state.allocBindings(0));
        auto v2 = findAlongAttrPath(state, settings.thisSystem, bindings, *v);

        return state.forceString(*v2);
    }
};

static RegisterCommand r1(make_ref<CmdUpgradeNix>());