aboutsummaryrefslogtreecommitdiff
path: root/stage1/lib/root.h
blob: c6470bccdfa60bbfb8e6e0e45644c1408ddf6ce7 (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
#include <filesystem>
#include <fstream>
#include <iostream>
#include <string>
#include <vector>

using std::string;
using std::vector;
namespace fs = std::filesystem;

/** Root build type */
enum r_type {
    FULL_NIXOS,
    FULL_DARWIN,
    PARTIAL,
    COLLECTION,
};

/** Root metadata */
struct root_t {
    string name;
    string path;
    enum r_type type;
};


vector<struct root_t> get_roots_for_path(string path)
{
    vector<struct root_t> roots;
    
    for(auto &entry: fs::directory_iterator(path)) {
        auto path = entry.path();

        if (path.extension() == ".nix") {
            // simple root
            roots.push_back(root_t {
                    path.filename(),
                    path.string(),
                    FULL_NIXOS,
                });
            
        } else {
            path /= "default.nix";
            bool exists = fs::exists(path);
            path = path.parent_path();
            
            if (exists) {
                // nested single root
                roots.push_back(root_t {
                        path.filename(),
                        path.string(),
                        FULL_NIXOS,
                    });
            } else {
                // nested single root
                roots.push_back(root_t {
                        path.filename(),
                        path.string(),
                        COLLECTION,
                    });
            }
        }
        
    }
    
    return roots;
}