Do not install to user profile

This commit is contained in:
dump_stack() 2020-01-04 10:52:16 +00:00
parent 49b8790032
commit d973179557
Signed by: dump_stack
GPG Key ID: BE44DA8C062D87DC

View File

@ -34,15 +34,8 @@ in {
} }
` `
func isPackageExists(name string) bool { func isPackageExists(channel, name string) bool {
// TODO use something like a return nil == exec.Command("nix-build", "<"+channel+">", "-A", name).Run()
// nix-build '<channel>' -A name
// to avoid installation to user profile
cmd := exec.Command("nix-env", "-iA", name)
cmd.Stdout = os.Stdout
cmd.Stderr = os.Stderr
err := cmd.Run()
return err == nil
} }
func nixPath(name string) (path string, err error) { func nixPath(name string) (path string, err error) {
@ -84,33 +77,37 @@ func filterDotfiles(files []os.FileInfo) (notHiddenFiles []os.FileInfo) {
} }
func generate(l *libvirt.Libvirt, pkg, bin, vmname string) (err error) { func generate(l *libvirt.Libvirt, pkg, bin, vmname string) (err error) {
var name string // TODO refactor
var name, channel string
if strings.Contains(pkg, ".") { if strings.Contains(pkg, ".") {
name = pkg channel = strings.Split(pkg, ".")[0]
name = strings.Join(strings.Split(pkg, ".")[1:], ".")
} else { } else {
log.Println("Package name does not contains channel") log.Println("Package name does not contains channel")
log.Println("Trying to guess") log.Println("Trying to guess")
var channel string
channel, err = guessChannel() channel, err = guessChannel()
if err != nil { if err != nil {
log.Println("Cannot guess channel") log.Println("Cannot guess channel")
log.Println("Check nix-channel --list") log.Println("Check nix-channel --list")
return log.Println("Will try <nixpkgs>")
channel = "nixpkgs"
err = nil
} }
name = channel + "." + pkg name = pkg
log.Println("Use", name) log.Println("Use", channel+"."+pkg)
} }
if !isPackageExists(name) { if !isPackageExists(channel, name) {
s := "Package " + name + " does not exists" s := "Package " + name + " does not exists"
err = errors.New(s) err = errors.New(s)
log.Println(s) log.Println(s)
return return
} }
path, err := nixPath(name) path, err := nixPath(channel + "." + name)
if err != nil { if err != nil {
log.Println("Cannot find nix path") log.Println("Cannot find nix path")
return return
@ -174,25 +171,14 @@ func generate(l *libvirt.Libvirt, pkg, bin, vmname string) (err error) {
bin = files[0].Name() bin = files[0].Name()
} }
var realName string
for i, s := range strings.Split(name, ".") {
if i == 0 {
continue
}
if i != 1 {
realName += "."
}
realName += s
}
var appFilename string var appFilename string
if vmname != "" { if vmname != "" {
appFilename = configDir + "/nix/" + vmname + ".nix" appFilename = configDir + "/nix/" + vmname + ".nix"
} else { } else {
appFilename = configDir + "/nix/" + realName + ".nix" appFilename = configDir + "/nix/" + name + ".nix"
} }
appNixConfig := fmt.Sprintf(template, realName, bin) appNixConfig := fmt.Sprintf(template, name, bin)
err = ioutil.WriteFile(appFilename, []byte(appNixConfig), 0600) err = ioutil.WriteFile(appFilename, []byte(appNixConfig), 0600)
if err != nil { if err != nil {