aboutsummaryrefslogtreecommitdiff
path: root/src/nix/show-derivation.cc
blob: 0799f21ebd6d93480e7a291350aff083aa235334 (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
124
125
126
127
// FIXME: integrate this with nix path-info?

#include "command.hh"
#include "common-args.hh"
#include "store-api.hh"
#include "archive.hh"
#include "json.hh"
#include "derivations.hh"

using namespace nix;

struct CmdShowDerivation : InstallablesCommand
{
    bool recursive = false;

    CmdShowDerivation()
    {
        addFlag({
            .longName = "recursive",
            .shortName = 'r',
            .description = "include the dependencies of the specified derivations",
            .handler = {&recursive, true}
        });
    }

    std::string description() override
    {
        return "show the contents of a store derivation";
    }

    Examples examples() override
    {
        return {
            Example{
                "To show the store derivation that results from evaluating the Hello package:",
                "nix show-derivation nixpkgs#hello"
            },
            Example{
                "To show the full derivation graph (if available) that produced your NixOS system:",
                "nix show-derivation -r /run/current-system"
            },
        };
    }

    Category category() override { return catUtility; }

    void run(ref<Store> store) override
    {
        auto drvPaths = toDerivations(store, installables, true);

        if (recursive) {
            StorePathSet closure;
            store->computeFSClosure(drvPaths, closure);
            drvPaths = std::move(closure);
        }

        {

        JSONObject jsonRoot(std::cout, true);

        for (auto & drvPath : drvPaths) {
            if (!drvPath.isDerivation()) continue;

            auto drvObj(jsonRoot.object(store->printStorePath(drvPath)));

            auto drv = store->readDerivation(drvPath);

            {
                auto outputsObj(drvObj.object("outputs"));
                for (auto & [_outputName, output] : drv.outputs) {
                    auto & outputName = _outputName; // work around clang bug
                    auto outputObj { outputsObj.object(outputName) };
                    std::visit(overloaded {
                        [&](DerivationOutputInputAddressed doi) {
                            outputObj.attr("path", store->printStorePath(doi.path));
                        },
                        [&](DerivationOutputCAFixed dof) {
                            outputObj.attr("path", store->printStorePath(dof.path(*store, drv.name, outputName)));
                            outputObj.attr("hashAlgo", printMethodAlgo(dof.ca));
                            outputObj.attr("hash", getContentAddressHash(dof.ca).to_string(Base16, false));
                            // FIXME print refs?
                        },
                        [&](DerivationOutputCAFloating dof) {
                            outputObj.attr("hashAlgo", makeContentAddressingPrefix(dof.method) + printHashType(dof.hashType));
                        },
                    }, output.output);
                }
            }

            {
                auto inputsList(drvObj.list("inputSrcs"));
                for (auto & input : drv.inputSrcs)
                    inputsList.elem(store->printStorePath(input));
            }

            {
                auto inputDrvsObj(drvObj.object("inputDrvs"));
                for (auto & input : drv.inputDrvs) {
                    auto inputList(inputDrvsObj.list(store->printStorePath(input.first)));
                    for (auto & outputId : input.second)
                        inputList.elem(outputId);
                }
            }

            drvObj.attr("platform", drv.platform);
            drvObj.attr("builder", drv.builder);

            {
                auto argsList(drvObj.list("args"));
                for (auto & arg : drv.args)
                    argsList.elem(arg);
            }

            {
                auto envObj(drvObj.object("env"));
                for (auto & var : drv.env)
                    envObj.attr(var.first, var.second);
            }
        }

        }

        std::cout << "\n";
    }
};

static auto rCmdShowDerivation = registerCommand<CmdShowDerivation>("show-derivation");