/
nix.go
189 lines (157 loc) · 4.99 KB
/
nix.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
package nix
import (
"bytes"
"fmt"
"io"
"os"
"os/exec"
"path/filepath"
"runtime"
"strings"
"github.com/benchkram/errz"
)
type Dependency struct {
// Name of the dependency
Name string
// Nixpkgs can be empty or a link to desired revision
// ex. https://github.com/NixOS/nixpkgs/archive/eeefd01d4f630fcbab6588fe3e7fffe0690fbb20.tar.gz
Nixpkgs string
}
type StorePath string
type DependenciesToStorePathMap map[Dependency]StorePath
// IsInstalled checks if nix is installed on the system
func IsInstalled() bool {
_, err := exec.LookPath("nix")
return err == nil
}
// BuildDependencies build nix dependencies and returns a <package>-<nix store path> map
//
// dependencies can be either a package name ex. php or a path to .nix file
// nixpkgs can be empty which means it will use local nixpkgs channel
// or a link to desired revision ex. https://github.com/NixOS/nixpkgs/archive/eeefd01d4f630fcbab6588fe3e7fffe0690fbb20.tar.gz
func BuildDependencies(deps []Dependency, cache *Cache) (_ DependenciesToStorePathMap, err error) {
defer errz.Recover(&err)
pkgToStorePath := make(DependenciesToStorePathMap)
for _, v := range deps {
var key string
if cache != nil {
key, err = GenerateKey(v)
errz.Fatal(err)
if storePath, ok := cache.Get(key); ok {
pkgToStorePath[v] = StorePath(storePath)
continue
}
}
if strings.HasSuffix(v.Name, ".nix") {
storePath, err := buildFile(v.Name, v.Nixpkgs)
if err != nil {
return DependenciesToStorePathMap{}, err
}
pkgToStorePath[v] = StorePath(storePath)
} else {
storePath, err := buildPackage(v.Name, v.Nixpkgs)
if err != nil {
return DependenciesToStorePathMap{}, err
}
pkgToStorePath[v] = StorePath(storePath)
}
if cache != nil {
err = cache.Save(key, string(pkgToStorePath[v]))
errz.Fatal(err)
}
}
return pkgToStorePath, nil
}
// buildPackage builds a nix package: nix-build --no-out-link -E 'with import <nixpkgs> { }; pkg' and returns the store path
func buildPackage(pkgName string, nixpkgs string) (string, error) {
nixExpression := fmt.Sprintf("with import %s { }; [%s]", source(nixpkgs), pkgName)
cmd := exec.Command("nix-build", "--no-out-link", "-E", nixExpression)
var stdoutBuf bytes.Buffer
cmd.Stdout = io.MultiWriter(os.Stdout, &stdoutBuf)
cmd.Stderr = os.Stderr
err := cmd.Run()
if err != nil {
return "", err
}
for _, v := range strings.Split(stdoutBuf.String(), "\n") {
if strings.HasPrefix(v, "/nix/store/") {
return v, nil
}
}
return "", nil
}
// buildFile builds a .nix expression file
// `nix-build --no-out-link -E 'with import <nixpkgs> { }; callPackage filepath.nix {}'`
func buildFile(filePath string, nixpkgs string) (string, error) {
nixExpression := fmt.Sprintf("with import %s { }; callPackage %s {}", source(nixpkgs), filePath)
cmd := exec.Command("nix-build", "--no-out-link", "-E", nixExpression)
var stdoutBuf bytes.Buffer
cmd.Stdout = io.MultiWriter(os.Stdout, &stdoutBuf)
cmd.Stderr = os.Stderr
err := cmd.Run()
if err != nil {
return "", err
}
for _, v := range strings.Split(stdoutBuf.String(), "\n") {
if strings.HasPrefix(v, "/nix/store/") {
return v, nil
}
}
return "", nil
}
// StorePathsBin adds the /bin dir to each of storePaths
func StorePathsBin(storePaths []string) []string {
out := make([]string, len(storePaths))
for i, sp := range storePaths {
out[i] = storePathBin(sp)
}
return out
}
// storePathBin adds the /bin dir to storePath
func storePathBin(storePath string) string {
return filepath.Join(storePath, "/bin")
}
// DownloadURl give nix download URL based on OS
func DownloadURl() string {
url := "https://nixos.org/download.html"
switch runtime.GOOS {
case "windows":
url = "https://nixos.org/download.html#nix-install-windows"
case "darwin":
url = "https://nixos.org/download.html#nix-install-macos"
case "linux":
url = "https://nixos.org/download.html#nix-install-linux"
}
return url
}
// DependenciesToStorePaths resolves a dependency array to their
// associated nix storePath. The order of the output is guaranteed
// to match the order of the input.
func DependenciesToStorePaths(dependencies []Dependency, m DependenciesToStorePathMap) ([]string, error) {
storePaths := make([]string, len(dependencies))
for i, d := range dependencies {
storePath, ok := m[d]
if !ok {
return nil, fmt.Errorf("could not resolve store path for [%s]", d)
}
storePaths[i] = string(storePath)
}
return storePaths, nil
}
// AddDir add the dir path to .nix files specified in dependencies
func AddDir(dir string, dependencies []string) []string {
for k, v := range dependencies {
if strings.HasSuffix(v, ".nix") {
dependencies[k] = dir + "/" + v
}
}
return dependencies
}
// Source of nixpkgs from where dependencies are built. If empty will use local <nixpkgs>
// or a specific tarball can be used ex. https://github.com/NixOS/nixpkgs/archive/eeefd01d4f630fcbab6588fe3e7fffe0690fbb20.tar.gz
func source(nixpkgs string) string {
if nixpkgs != "" {
return fmt.Sprintf("(fetchTarball \"%s\")", nixpkgs)
}
return "<nixpkgs>"
}