[feat] local config evaluation; getting repos while using remote configs.
This commit is contained in:
parent
24927bc787
commit
4104d91eab
73
appvm.go
73
appvm.go
@ -8,6 +8,7 @@
|
|||||||
package main
|
package main
|
||||||
|
|
||||||
import (
|
import (
|
||||||
|
"bytes"
|
||||||
"errors"
|
"errors"
|
||||||
"fmt"
|
"fmt"
|
||||||
"io"
|
"io"
|
||||||
@ -30,24 +31,31 @@ import (
|
|||||||
kingpin "gopkg.in/alecthomas/kingpin.v2"
|
kingpin "gopkg.in/alecthomas/kingpin.v2"
|
||||||
)
|
)
|
||||||
|
|
||||||
func evalNix(expr string) (s string) {
|
func evalNix(expr string) (value string, err error) {
|
||||||
command := exec.Command("nix", "eval", "--raw", expr)
|
command := exec.Command("nix", "eval", "--raw", "(toString ("+expr+"))")
|
||||||
bytes, _ := command.Output()
|
log.Print("Evaluating " + expr)
|
||||||
s = string(bytes)
|
var stdout bytes.Buffer
|
||||||
|
command.Stdout = &stdout
|
||||||
|
err = command.Run()
|
||||||
|
value = stdout.String()
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
|
|
||||||
// Gets an expression returning AppVM config path
|
// Gets an expression returning AppVM config path
|
||||||
func getAppVMExpressionPath(name string) string {
|
func getAppVMExpressionPath(name string) (paths []string, config string) {
|
||||||
paths := strings.Split(os.Getenv("APPVM_CONFIGS"), ":")
|
paths = strings.Split(os.Getenv("NIX_PATH"), ":")
|
||||||
for _, a := range paths {
|
config = "nix/" + name + ".nix"
|
||||||
searchpath := a + "/nix"
|
|
||||||
log.Print("Searching " + searchpath + " for expressions")
|
|
||||||
if _, err := os.Stat(searchpath); os.IsExist(err) {
|
|
||||||
exprpath := searchpath + "/" + name + ".nix"
|
|
||||||
|
|
||||||
if os.Stat(exprpath); os.IsExist(err) {
|
for _, a := range paths {
|
||||||
return exprpath
|
searchpath := a
|
||||||
|
log.Print("Searching " + searchpath + " for expressions")
|
||||||
|
if _, err := os.Stat(searchpath); !os.IsNotExist(err) {
|
||||||
|
exprpath := searchpath + "/nix/" + name + ".nix"
|
||||||
|
log.Print("File exists?: ", exprpath)
|
||||||
|
if _, err := os.Stat(exprpath); !os.IsNotExist(err) {
|
||||||
|
log.Print("File exists: ", exprpath)
|
||||||
|
config = exprpath
|
||||||
|
return
|
||||||
}
|
}
|
||||||
|
|
||||||
}
|
}
|
||||||
@ -55,15 +63,26 @@ func getAppVMExpressionPath(name string) string {
|
|||||||
}
|
}
|
||||||
log.Print("Trying to use remote repo config")
|
log.Print("Trying to use remote repo config")
|
||||||
|
|
||||||
fetchFormat := "(builtins.fetchurl \"raw.githubusercontent.com/%[1]s/%[2]s/master/nix/%[3]s.nix\" )"
|
rev := "1251a21abadc15b7187d485317434c9befd0f6b2"
|
||||||
|
fetchFormat := "(builtins.fetchTarball \"https://github.com/%[1]s/%[2]s/archive/" + rev + ".tar.gz\")"
|
||||||
splitString := strings.Split(name, "/")
|
splitString := strings.Split(name, "/")
|
||||||
|
|
||||||
if len(splitString) != 3 {
|
if len(splitString) != 3 {
|
||||||
// nope, not a repo format
|
// nope, not a repo format
|
||||||
return evalNix(fmt.Sprintf(fetchFormat, "jollheef", "appvm", name))
|
path, err := evalNix(fmt.Sprintf(fetchFormat, "jollheef", "appvm", name))
|
||||||
|
|
||||||
|
if err != nil {
|
||||||
|
log.Fatal("OH NO ", err)
|
||||||
|
return
|
||||||
|
}
|
||||||
|
|
||||||
|
paths = []string{path}
|
||||||
|
config = oomph + "/nix/" + name + ".nix"
|
||||||
|
|
||||||
|
return
|
||||||
}
|
}
|
||||||
|
|
||||||
return evalNix(fmt.Sprintf(fetchFormat, splitString[0], splitString[1], splitString[2]))
|
return
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -139,11 +158,25 @@ func streamStdOutErr(command *cmd.Cmd) {
|
|||||||
}
|
}
|
||||||
|
|
||||||
func generateVM(name string, verbose bool) (realpath, reginfo, qcow2 string, err error) {
|
func generateVM(name string, verbose bool) (realpath, reginfo, qcow2 string, err error) {
|
||||||
vmConfigPath := getAppVMExpressionPath(name)
|
paths, config := getAppVMExpressionPath(name)
|
||||||
log.Print(vmConfigPath)
|
|
||||||
|
nixargs := []string{
|
||||||
|
"<nixpkgs/nixos>",
|
||||||
|
"-A", "config.system.build.vm",
|
||||||
|
"-I", "nixos-config=" + config,
|
||||||
|
// Taking local config from home folder
|
||||||
|
"-I" + os.Getenv("HOME"),
|
||||||
|
}
|
||||||
|
|
||||||
|
for _, path := range paths {
|
||||||
|
nixargs = append(nixargs, "-I", path)
|
||||||
|
}
|
||||||
|
|
||||||
command := cmd.NewCmdOptions(cmd.Options{Buffered: false, Streaming: true},
|
command := cmd.NewCmdOptions(cmd.Options{Buffered: false, Streaming: true},
|
||||||
"nix-build", "<nixpkgs/nixos>", "-A", "config.system.build.vm",
|
"nix-build",
|
||||||
"-I", "nixos-config="+vmConfigPath, "-I", configDir)
|
nixargs...,
|
||||||
|
)
|
||||||
|
|
||||||
if verbose {
|
if verbose {
|
||||||
go streamStdOutErr(command)
|
go streamStdOutErr(command)
|
||||||
}
|
}
|
||||||
|
Loading…
Reference in New Issue
Block a user