-
Notifications
You must be signed in to change notification settings - Fork 0
/
main.go
161 lines (141 loc) · 3.6 KB
/
main.go
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
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
package main
import (
"fmt"
"log"
"os"
"os/exec"
"runtime"
"sort"
"strings"
"github.com/urfave/cli/v2"
)
var platforms = map[string]string{
"arm64": "aarch64",
}
var platform = fmt.Sprintf("%s-%s", platforms[runtime.GOARCH], runtime.GOOS)
func build_pkg_path(name string) string {
return fmt.Sprintf("legacyPackages.%s.%s", platform, name)
}
func get_local_pkgs() []string {
output_bytes, err := exec.Command("nix", "profile", "list").Output()
if err != nil {
log.Fatal(err)
}
output_lines := strings.Split(strings.TrimRight(string(output_bytes[:]), "\n"), "\n")
var names []string
for _, line := range output_lines {
pkg_path := strings.Split(line, " ")[1]
splits := strings.Split(pkg_path, ".")
pkg_name := splits[len(splits)-1]
names = append(names, pkg_name)
}
return names
}
func already_installed(name string) bool {
for _, item := range get_local_pkgs() {
if item == name {
return true
}
}
return false
}
func main() {
app := &cli.App{
EnableBashCompletion: true,
Commands: []*cli.Command{
{
Name: "install",
Aliases: []string{"i"},
Usage: "install package from nixpkgs to nix default profile",
Action: func(cCtx *cli.Context) error {
name := cCtx.Args().First()
if already_installed(name) {
fmt.Println("[Skipped]", name, "is already installed.")
return nil
}
fmt.Println("installing", name)
pkg := "nixpkgs#" + name
cmd := exec.Command("nix", "profile", "install", pkg)
cmd.Stdout = os.Stdout
if err := cmd.Run(); err != nil {
log.Fatal(err)
}
return nil
},
},
{
Name: "list",
Aliases: []string{"l"},
Usage: "list installed packages in nix default profile",
Action: func(cCtx *cli.Context) error {
names := get_local_pkgs()
sort.Slice(names, func(x, y int) bool {
return names[x] < names[y]
})
for _, name := range names {
fmt.Println(name)
}
return nil
},
},
{
Name: "upgrade",
Aliases: []string{"u"},
Usage: "upgrade installed package in nix default profile",
Action: func(cCtx *cli.Context) error {
pkg_name := cCtx.Args().First()
fmt.Println("upgrading", pkg_name)
cmd := exec.Command("nix", "profile", "upgrade", build_pkg_path(pkg_name))
cmd.Stdout = os.Stdout
if err := cmd.Run(); err != nil {
log.Fatal(err)
}
return nil
},
},
{
Name: "remove",
Aliases: []string{"r"},
Usage: "remove installed package from nix default profile",
Action: func(cCtx *cli.Context) error {
pkg_name := cCtx.Args().First()
fmt.Println("removing", pkg_name)
cmd := exec.Command("nix", "profile", "remove", build_pkg_path(pkg_name))
cmd.Stdout = os.Stdout
if err := cmd.Run(); err != nil {
log.Fatal(err)
}
return nil
},
},
{
Name: "clean-up",
Usage: "clean up duplicate packages from nix default profile",
Action: func(cCtx *cli.Context) error {
names := get_local_pkgs()
names_map := map[string]int{}
for _, name := range names {
if _, ok := names_map[name]; ok {
cmd := exec.Command("nix", "profile", "remove", build_pkg_path(name))
cmd.Stdout = os.Stdout
if err := cmd.Run(); err != nil {
log.Fatal(err)
}
cmd = exec.Command("nix", "profile", "install", "nixpkgs#"+name)
cmd.Stdout = os.Stdout
if err := cmd.Run(); err != nil {
log.Fatal(err)
}
} else {
names_map[name] = 1
}
}
return nil
},
},
},
}
if err := app.Run(os.Args); err != nil {
log.Fatal(err)
}
}