Skip to content

Commit

Permalink
rename to nixose and move package to root
Browse files Browse the repository at this point in the history
  • Loading branch information
aksiksi committed Nov 4, 2023
1 parent 06828c2 commit 45172b3
Show file tree
Hide file tree
Showing 10 changed files with 85 additions and 84 deletions.
2 changes: 1 addition & 1 deletion Makefile
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
build:
mkdir -p bin/ && go build -o bin/ ./cmd/compose2nixos
mkdir -p bin/ && go build -o bin/ ./cmd/nixose

.PHONY: build run
23 changes: 14 additions & 9 deletions cmd/compose2nixos/main.go → cmd/nixose/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,19 +3,21 @@ package main
import (
"context"
"flag"
"fmt"
"log"
"os"
"strings"
"time"

compose2nixos "github.com/aksiksi/compose2nixos/pkg/lib"
"github.com/aksiksi/nixose"
)

var paths = flag.String("paths", "", "paths to Compose files")
var envFiles = flag.String("env_files", "", "paths to .env files")
var envFilesOnly = flag.Bool("env_files_only", false, "only use env files in the NixOS container definitions")
var output = flag.String("output", "", "path to output Nix file")
var project = flag.String("project", "", "project name used as a prefix for generated resources")
var projectSeparator = flag.String("project_separator", compose2nixos.DefaultProjectSeparator, "seperator for project prefix")
var projectSeparator = flag.String("project_separator", nixose.DefaultProjectSeparator, "seperator for project prefix")
var autoStart = flag.Bool("auto_start", true, "control auto-start setting for containers")
var runtime = flag.String("runtime", "podman", `"podman" or "docker"`)

Expand All @@ -25,23 +27,24 @@ func main() {
ctx := context.Background()

if *paths == "" {
log.Fatalf("one or more paths must be specified")
log.Fatalf("One or more paths must be specified")
}

paths := strings.Split(*paths, ",")
envFiles := strings.Split(*envFiles, ",")

var containerRuntime compose2nixos.ContainerRuntime
var containerRuntime nixose.ContainerRuntime
if *runtime == "podman" {
containerRuntime = compose2nixos.ContainerRuntimePodman
containerRuntime = nixose.ContainerRuntimePodman
} else if *runtime == "docker" {
containerRuntime = compose2nixos.ContainerRuntimeDocker
containerRuntime = nixose.ContainerRuntimeDocker
} else {
log.Fatalf("invalid --runtime: %q", *runtime)
log.Fatalf("Invalid --runtime: %q", *runtime)
}

g := compose2nixos.Generator{
Project: compose2nixos.NewProject(*project, *projectSeparator),
start := time.Now()
g := nixose.Generator{
Project: nixose.NewProject(*project, *projectSeparator),
Runtime: containerRuntime,
Paths: paths,
EnvFiles: envFiles,
Expand All @@ -52,10 +55,12 @@ func main() {
if err != nil {
log.Fatal(err)
}
fmt.Printf("Generated NixOS config in %v\n", time.Since(start))

if *output != "" {
if err := os.WriteFile(*output, []byte(containerConfig.String()), os.FileMode(0644)); err != nil {
log.Fatal(err)
}
fmt.Printf("Wrote NixOS config to %s\n", *output)
}
}
53 changes: 2 additions & 51 deletions pkg/lib/compose.go → compose.go
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
package compose2nixos
package nixose

import (
"cmp"
Expand All @@ -13,29 +13,6 @@ import (
"golang.org/x/exp/maps"
)

const DefaultProjectSeparator = "-"

type ContainerRuntime int

const (
ContainerRuntimeInvalid ContainerRuntime = iota
ContainerRuntimeDocker
ContainerRuntimePodman
)

func (c ContainerRuntime) String() string {
switch c {
case ContainerRuntimeDocker:
return "docker"
case ContainerRuntimePodman:
return "podman"
case ContainerRuntimeInvalid:
return "invalid-container-runtime"
default:
panic("unreachable")
}
}

func composeEnvironmentToMap(env types.MappingWithEquals) map[string]string {
m := make(map[string]string)
for k, v := range env {
Expand Down Expand Up @@ -70,32 +47,6 @@ func portConfigsToPortStrings(portConfigs []types.ServicePortConfig) []string {
return ports
}

type Project struct {
name string
separator string
}

func NewProject(name, separator string) *Project {
if name == "" {
return nil
}
if separator == "" {
separator = DefaultProjectSeparator
}
return &Project{name, separator}
}

func (c *Project) Name() string {
return c.name
}

func (c *Project) With(s string) string {
if c == nil {
return s
}
return fmt.Sprintf("%s%s%s", c.name, c.separator, s)
}

type Generator struct {
Project *Project
Runtime ContainerRuntime
Expand Down Expand Up @@ -228,7 +179,7 @@ func (g *Generator) buildNixVolumes(containers []NixContainer) []NixVolume {
if g.Runtime == ContainerRuntimePodman && v.Driver == "" {
bindPath := v.DriverOpts["device"]
if bindPath == "" {
log.Fatalf("volume %q has no device set", name)
log.Fatalf("Volume %q has no device set", name)
}
for _, c := range containers {
if volumeString, ok := c.Volumes[name]; ok {
Expand Down
2 changes: 1 addition & 1 deletion go.mod
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
module github.com/aksiksi/compose2nixos
module github.com/aksiksi/nixose

go 1.21

Expand Down
2 changes: 1 addition & 1 deletion pkg/lib/helpers.go → helpers.go
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
package compose2nixos
package nixose

import (
"bufio"
Expand Down
47 changes: 46 additions & 1 deletion pkg/lib/nix.go → nixose.go
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
package compose2nixos
package nixose

import (
"embed"
Expand Down Expand Up @@ -34,6 +34,51 @@ var funcMap template.FuncMap = template.FuncMap{
"mapToKeyValArray": mapToKeyValArray,
}

const DefaultProjectSeparator = "-"

type ContainerRuntime int

const (
ContainerRuntimeInvalid ContainerRuntime = iota
ContainerRuntimeDocker
ContainerRuntimePodman
)

func (c ContainerRuntime) String() string {
switch c {
case ContainerRuntimeDocker:
return "docker"
case ContainerRuntimePodman:
return "podman"
case ContainerRuntimeInvalid:
return "invalid-container-runtime"
default:
panic("Unreachable")
}
}

type Project struct {
Name string
separator string
}

func NewProject(name, separator string) *Project {
if name == "" {
return nil
}
if separator == "" {
separator = DefaultProjectSeparator
}
return &Project{name, separator}
}

func (p *Project) With(name string) string {
if p == nil {
return name
}
return fmt.Sprintf("%s%s%s", p.Name, p.separator, name)
}

type NixNetwork struct {
Project *Project
Runtime ContainerRuntime
Expand Down
16 changes: 8 additions & 8 deletions pkg/lib/templates/container.tmpl → templates/container.tmpl
Original file line number Diff line number Diff line change
Expand Up @@ -10,59 +10,59 @@ virtualisation.oci-containers.containers."{{$name}}" = {
{{$k}} = "{{$v}}";
{{- end}}
};
{{- end -}}
{{- end}}

{{- if .EnvFiles}}
environmentFiles = [
{{- range .EnvFiles}}
"${./{{.}}}"
{{- end}}
];
{{- end -}}
{{- end}}

{{- if .Volumes}}
volumes = [
{{- range $k, $v := .Volumes}}
"{{$v}}"
{{- end}}
];
{{- end -}}
{{- end}}

{{- if .Ports}}
ports = [
{{- range .Ports}}
"{{.}}"
{{- end}}
];
{{- end -}}
{{- end}}

{{- if .Labels}}
labels = {
{{- range $k, $v := .Labels}}
"{{$k}}" = "{{$v}}";
{{- end}}
};
{{- end -}}
{{- end}}

{{- if .DependsOn}}
dependsOn = [
{{- range .DependsOn}}
"{{.}}"
{{- end}}
];
{{- end -}}
{{- end}}

{{- if .ExtraOptions}}
extraOptions = [
{{- range .ExtraOptions}}
"{{.}}"
{{- end}}
];
{{- end -}}
{{- end}}

{{- if .User}}
user = "{{.User}}";
{{- end -}}
{{- end}}

{{- if not .AutoStart}}
autoStart = false;
Expand Down
File renamed without changes.
14 changes: 7 additions & 7 deletions pkg/lib/templates/network.tmpl → templates/network.tmpl
Original file line number Diff line number Diff line change
Expand Up @@ -4,20 +4,20 @@
systemd.services."create-{{$runtime}}-network-{{$name}}" = {
serviceConfig.Type = "oneshot";
path = [ pkgs.{{$runtime}} ];
{{if eq $runtime "docker" -}}
{{- if eq $runtime "docker"}}
script = ''
docker network inspect {{$name}} || docker network create {{$name}}{{ $labels | join " "}}
'';
{{else -}}
{{- else}}
script = ''
podman network create {{$name}} --opt isolate=true --ignore{{ $labels | join " "}}
'';
{{end -}}
{{if .Containers -}}
{{- end}}
{{- if .Containers}}
wantedBy = [
{{range .Containers}}
{{- . | printf "\"%s.service\"" | indent 2}}
{{end -}}
{{- range .Containers}}
{{. | printf "\"%s.service\"" | indent 2}}
{{- end}}
];
{{- end}}
};
10 changes: 5 additions & 5 deletions pkg/lib/templates/volume.tmpl → templates/volume.tmpl
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@
systemd.services."create-{{$runtime}}-volume-{{$name}}" = {
serviceConfig.Type = "oneshot";
path = [ pkgs.{{$runtime}} ];
{{if eq $runtime "docker" -}}
{{- if eq $runtime "docker"}}
script = ''
{{- if eq .Driver ""}}
docker volume inspect {{$name}} || docker volume create {{$name}} --opt {{$driverOptsString}}
Expand All @@ -21,12 +21,12 @@ systemd.services."create-{{$runtime}}-volume-{{$name}}" = {
podman volume create {{$name}} --driver {{.Driver}} --opt {{$driverOptsString}} --ignore
{{- end}}
'';
{{end}}
{{- end}}
{{- if .Containers}}
wantedBy = [
{{range .Containers}}
{{- . | printf "\"%s.service\"" | indent 2}}
{{end -}}
{{- range .Containers}}
{{. | printf "\"%s.service\"" | indent 2}}
{{- end}}
];
{{- end}}
};

0 comments on commit 45172b3

Please sign in to comment.