-
Notifications
You must be signed in to change notification settings - Fork 18
/
generate.go
205 lines (175 loc) · 4.02 KB
/
generate.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
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
package main
import (
"errors"
"fmt"
"io/ioutil"
"log"
"os"
"os/exec"
"strings"
)
var template = `
{pkgs, ...}:
let
application = "${pkgs.%s}/bin/%s";
appRunner = pkgs.writeShellScriptBin "app" ''
ARGS_FILE=/home/user/.args
ARGS=$(cat $ARGS_FILE)
rm $ARGS_FILE
${application} $ARGS
systemctl poweroff
'';
in {
imports = [
<nixpkgs/nixos/modules/virtualisation/qemu-vm.nix>
<nix/base.nix>
];
services.xserver.displayManager.sessionCommands = "${appRunner}/bin/app &";
}
`
func isPackageExists(channel, name string) bool {
return nil == exec.Command("nix-build", "<"+channel+">", "-A", name).Run()
}
func nixPath(name string) (path string, err error) {
command := exec.Command("nix", "path-info", name)
bytes, err := command.Output()
if err != nil {
return
}
path = string(bytes)
return
}
func guessChannel() (channel string, err error) {
command := exec.Command("nix-channel", "--list")
bytes, err := command.Output()
if err != nil {
return
}
channels := strings.Split(string(bytes), "\n")
for _, line := range channels {
fields := strings.Fields(line)
if len(fields) == 2 {
if strings.Contains(fields[1], "nixos.org/channels") {
channel = fields[0]
return
}
}
}
err = errors.New("No channel found")
return
}
func filterDotfiles(files []os.FileInfo) (notHiddenFiles []os.FileInfo) {
for _, f := range files {
if !strings.HasPrefix(f.Name(), ".") {
notHiddenFiles = append(notHiddenFiles, f)
}
}
return
}
func generate(pkg, bin, vmname string, build bool) (err error) {
// TODO refactor
var name, channel string
if strings.Contains(pkg, ".") {
channel = strings.Split(pkg, ".")[0]
name = strings.Join(strings.Split(pkg, ".")[1:], ".")
} else {
log.Println("Package name does not contains channel")
log.Println("Trying to guess")
channel, err = guessChannel()
if err != nil {
log.Println("Cannot guess channel")
log.Println("Check nix-channel --list")
log.Println("Will try <nixpkgs>")
channel = "nixpkgs"
err = nil
}
name = pkg
log.Println("Use", channel+"."+pkg)
}
if !isPackageExists(channel, name) {
s := "Package " + name + " does not exists"
err = errors.New(s)
log.Println(s)
return
}
path, err := nixPath(channel + "." + name)
if err != nil {
log.Println("Cannot find nix path")
return
}
path = strings.TrimSpace(path)
files, err := ioutil.ReadDir(path + "/bin/")
if err != nil {
log.Println(err)
return
}
if bin == "" && len(files) != 1 {
fmt.Println("There's more than one binary in */bin")
fmt.Println("Files in", path+"/bin/:")
for _, f := range files {
fmt.Println("\t", f.Name())
}
log.Println("Trying to guess binary")
var found bool = false
notHiddenFiles := filterDotfiles(files)
if len(notHiddenFiles) == 1 {
log.Println("Use", notHiddenFiles[0].Name())
bin = notHiddenFiles[0].Name()
found = true
}
if !found {
for _, f := range files {
parts := strings.Split(pkg, ".")
if f.Name() == parts[len(parts)-1] {
log.Println("Use", f.Name())
bin = f.Name()
found = true
}
}
}
if !found {
log.Println("Cannot guess in */bin, " +
"you should specify one of them explicitly")
return
}
}
if bin != "" {
var found bool = false
for _, f := range files {
if bin == f.Name() {
found = true
}
}
if !found {
log.Println("There's no such file in */bin")
return
}
} else {
bin = files[0].Name()
}
var appFilename string
if vmname != "" {
appFilename = configDir + "/nix/" + vmname + ".nix"
} else {
appFilename = configDir + "/nix/" + name + ".nix"
}
appNixConfig := fmt.Sprintf(template, name, bin)
err = ioutil.WriteFile(appFilename, []byte(appNixConfig), 0600)
if err != nil {
log.Println(err)
return
}
fmt.Print(appNixConfig + "\n")
log.Println("Configuration file is saved to", appFilename)
if build {
if vmname != "" {
_, _, _, err = generateVM(configDir, vmname, true)
} else {
_, _, _, err = generateVM(configDir, name, true)
}
if err != nil {
return
}
}
return
}