aboutsummaryrefslogtreecommitdiff
path: root/src/nix/flake.cc
blob: a5a1d34db503de34965ef513f5b6614da55ba60e (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
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
#include "primops/flake.hh"
#include "command.hh"
#include "common-args.hh"
#include "shared.hh"
#include "progress-bar.hh"
#include "eval.hh"
#include <nlohmann/json.hpp>

using namespace nix;

struct CmdFlakeList : StoreCommand, MixEvalArgs
{
    std::string name() override
    {
        return "list";
    }

    std::string description() override
    {
        return "list available Nix flakes";
    }

    void run(nix::ref<nix::Store> store) override
    {
        auto evalState = std::make_shared<EvalState>(searchPath, store);

        auto registry = evalState->getFlakeRegistry();

        stopProgressBar();

        for (auto & entry : registry.entries) {
            std::cout << entry.first << " " << entry.second.ref.to_string() << "\n";
        }
    }
};

struct CmdFlakeUpdate : StoreCommand, GitRepoCommand, MixEvalArgs
{
    std::string name() override
    {
        return "update";
    }

    std::string description() override
    {
        return "update flake lock file";
    }

    void run(nix::ref<nix::Store> store) override
    {
        auto evalState = std::make_shared<EvalState>(searchPath, store);

        if (flakeUri == "") flakeUri = absPath("./flake.nix");
        int result = updateLockFile(*evalState, flakeUri);
        if (result == 1) {
            std::cout << "You can only update local flakes, not flakes on GitHub.\n";
        } else if (result == 2) {
            std::cout << "You can only update local flakes, not flakes through their FlakeId.\n";
        }
    }
};

struct CmdFlakeInfo : FlakeCommand, MixJSON
{
    std::string name() override
    {
        return "info";
    }

    std::string description() override
    {
        return "list info about a given flake";
    }

    void run(nix::ref<nix::Store> store) override
    {
        auto evalState = std::make_shared<EvalState>(searchPath, store);
        nix::Flake flake = nix::getFlake(*evalState, FlakeRef(flakeUri));
        if (json) {
            nlohmann::json j;
            j["location"] = flake.path;
            j["description"] = flake.description;
            std::cout << j.dump(4) << std::endl;
        } else {
            std::cout << "Description: " << flake.description << "\n";
            std::cout << "Location:    " << flake.path << "\n";
        }
    }
};

struct CmdFlake : virtual MultiCommand, virtual Command
{
    CmdFlake()
        : MultiCommand({make_ref<CmdFlakeList>()
            , make_ref<CmdFlakeInfo>()
            , make_ref<CmdFlakeUpdate>()})
    {
    }

    std::string name() override
    {
        return "flake";
    }

    std::string description() override
    {
        return "manage Nix flakes";
    }

    void run() override
    {
        if (!command)
            throw UsageError("'nix flake' requires a sub-command.");
        command->run();
    }

    void printHelp(const string & programName, std::ostream & out) override
    {
        MultiCommand::printHelp(programName, out);
    }
};

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