Skip to content

Commit

Permalink
WIP: entrypoint
Browse files Browse the repository at this point in the history
  • Loading branch information
aksiksi committed Sep 27, 2024
1 parent 7c53c2f commit 7689b33
Show file tree
Hide file tree
Showing 11 changed files with 305 additions and 4 deletions.
3 changes: 3 additions & 0 deletions compose.go
Original file line number Diff line number Diff line change
Expand Up @@ -330,6 +330,9 @@ func (g *Generator) buildNixContainer(service types.ServiceConfig, networkMap ma
if !service.Command.IsZero() {
c.Command = service.Command
}
if entrypoint := service.Entrypoint; !entrypoint.IsZero() {
c.ExtraOptions = append(c.ExtraOptions, fmt.Sprintf("--entrypoint=%s", sliceToStringArray(entrypoint)))
}

// Figure out explicit dependencies for this container.
//
Expand Down
9 changes: 9 additions & 0 deletions helpers.go
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,15 @@ func mapToRepeatedKeyValFlag(flagName string, m map[string]string) []string {
return arr
}

func sliceToStringArray(s []string) string {
b := strings.Builder{}
b.WriteString("[")
for i := range s {
s[i] = fmt.Sprintf("%q", s[i])
}
return fmt.Sprintf("[%s]", strings.Join(s, ", "))
}

// ReadEnvFiles reads the given set of env files into a list of KEY=VAL entries.
//
// If mergeWithEnv is set, the running env is merged with the provided env files. Any
Expand Down
10 changes: 10 additions & 0 deletions nix_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -144,6 +144,16 @@ func TestUpheldBy(t *testing.T) {
runSubtestsWithGenerator(t, g)
}

func TestCommandAndEntrypoint(t *testing.T) {
composePath, envFilePath := getPaths(t, false)
g := &Generator{
Project: NewProject("test"),
Inputs: []string{composePath},
EnvFiles: []string{envFilePath},
}
runSubtestsWithGenerator(t, g)
}

func TestRemoveVolumes(t *testing.T) {
composePath, envFilePath := getPaths(t, true)
g := &Generator{
Expand Down
26 changes: 26 additions & 0 deletions nixos-test/docker-compose.nix
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,32 @@
virtualisation.oci-containers.backend = "docker";

# Containers
virtualisation.oci-containers.containers."myproject-entrypoint" = {
image = "docker.io/library/nginx:stable-alpine-slim";
log-driver = "journald";
extraOptions = [
"--entrypoint=["echo", "abc"]"
"--network-alias=entrypoint"
"--network=myproject_default"
];
};
systemd.services."docker-myproject-entrypoint" = {
serviceConfig = {
Restart = lib.mkOverride 90 "no";
};
after = [
"docker-network-myproject_default.service"
];
requires = [
"docker-network-myproject_default.service"
];
partOf = [
"docker-compose-myproject-root.target"
];
wantedBy = [
"docker-compose-myproject-root.target"
];
};
virtualisation.oci-containers.containers."myproject-no-restart" = {
image = "docker.io/library/nginx:stable-alpine-slim";
log-driver = "journald";
Expand Down
5 changes: 5 additions & 0 deletions nixos-test/docker-compose.yml
Original file line number Diff line number Diff line change
Expand Up @@ -42,6 +42,11 @@ services:
restart: on-failure:3
no-restart:
image: docker.io/library/nginx:stable-alpine-slim
entrypoint:
image: docker.io/library/nginx:stable-alpine-slim
entrypoint:
- echo
- abc

networks:
something:
Expand Down
26 changes: 26 additions & 0 deletions nixos-test/podman-compose.nix
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,32 @@
virtualisation.oci-containers.backend = "podman";

# Containers
virtualisation.oci-containers.containers."myproject-entrypoint" = {
image = "docker.io/library/nginx:stable-alpine-slim";
log-driver = "journald";
extraOptions = [
"--entrypoint=["echo", "abc"]"
"--network-alias=entrypoint"
"--network=myproject_default"
];
};
systemd.services."podman-myproject-entrypoint" = {
serviceConfig = {
Restart = lib.mkOverride 90 "no";
};
after = [
"podman-network-myproject_default.service"
];
requires = [
"podman-network-myproject_default.service"
];
partOf = [
"podman-compose-myproject-root.target"
];
wantedBy = [
"podman-compose-myproject-root.target"
];
};
virtualisation.oci-containers.containers."myproject-no-restart" = {
image = "docker.io/library/nginx:stable-alpine-slim";
log-driver = "journald";
Expand Down
11 changes: 8 additions & 3 deletions template.go
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,10 @@ func derefInt(v *int) int {
return *v
}

func derefString(v *string) string {
return *v

Check warning on line 29 in template.go

View check run for this annotation

Codecov / codecov/patch

template.go#L28-L29

Added lines #L28 - L29 were not covered by tests
}

func toNixValue(v any) any {
switch v := v.(type) {
case string:
Expand All @@ -46,7 +50,8 @@ func toNixList(s []string) string {
}

var funcMap template.FuncMap = template.FuncMap{
"derefInt": derefInt,
"toNixValue": toNixValue,
"toNixList": toNixList,
"derefInt": derefInt,
"derefString": derefString,
"toNixValue": toNixValue,
"toNixList": toNixList,
}
2 changes: 1 addition & 1 deletion templates/container.nix.tmpl
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,7 @@ virtualisation.oci-containers.containers."{{.Name}}" = {
];
{{- end}}

{{- if .Command}}
{{- if ne .Command nil}}
cmd = {{toNixList .Command}};
{{- end}}

Expand Down
12 changes: 12 additions & 0 deletions testdata/TestCommandAndEntrypoint.compose.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
services:
all:
image: nginx:latest
command: ["ls", "-la"]
entrypoint:
["nginx", "-g", "daemon off;", "-c", "/etc/config/nginx/conf/nginx.conf"]
empty-command:
image: nginx:latest
command: []
empty-entrypoint:
image: nginx:latest
entrypoint: []
100 changes: 100 additions & 0 deletions testdata/TestCommandAndEntrypoint.docker.nix
Original file line number Diff line number Diff line change
@@ -0,0 +1,100 @@
{ pkgs, lib, ... }:

{
# Runtime
virtualisation.docker = {
enable = true;
autoPrune.enable = true;
};
virtualisation.oci-containers.backend = "docker";

# Containers
virtualisation.oci-containers.containers."test-all" = {
image = "nginx:latest";
cmd = [ "ls" "-la" ];
log-driver = "journald";
autoStart = false;
extraOptions = [
"--entrypoint=["nginx", "-g", "daemon off;", "-c", "/etc/config/nginx/conf/nginx.conf"]"
"--network-alias=all"
"--network=test_default"
];
};
systemd.services."docker-test-all" = {
serviceConfig = {
Restart = lib.mkOverride 90 "no";
};
after = [
"docker-network-test_default.service"
];
requires = [
"docker-network-test_default.service"
];
};
virtualisation.oci-containers.containers."test-empty-command" = {
image = "nginx:latest";
cmd = [ ];
log-driver = "journald";
autoStart = false;
extraOptions = [
"--network-alias=empty-command"
"--network=test_default"
];
};
systemd.services."docker-test-empty-command" = {
serviceConfig = {
Restart = lib.mkOverride 90 "no";
};
after = [
"docker-network-test_default.service"
];
requires = [
"docker-network-test_default.service"
];
};
virtualisation.oci-containers.containers."test-empty-entrypoint" = {
image = "nginx:latest";
log-driver = "journald";
autoStart = false;
extraOptions = [
"--entrypoint=[]"
"--network-alias=empty-entrypoint"
"--network=test_default"
];
};
systemd.services."docker-test-empty-entrypoint" = {
serviceConfig = {
Restart = lib.mkOverride 90 "no";
};
after = [
"docker-network-test_default.service"
];
requires = [
"docker-network-test_default.service"
];
};
# Networks
systemd.services."docker-network-test_default" = {
path = [ pkgs.docker ];
serviceConfig = {
Type = "oneshot";
RemainAfterExit = true;
ExecStop = "docker network rm -f test_default";
};
script = ''
docker network inspect test_default || docker network create test_default
'';
partOf = [ "docker-compose-test-root.target" ];
wantedBy = [ "docker-compose-test-root.target" ];
};
# Root service
# When started, this will automatically create all resources and start
# the containers. When stopped, this will teardown all resources.
systemd.targets."docker-compose-test-root" = {
unitConfig = {
Description = "Root target generated by compose2nix.";
};
};
}
105 changes: 105 additions & 0 deletions testdata/TestCommandAndEntrypoint.podman.nix
Original file line number Diff line number Diff line change
@@ -0,0 +1,105 @@
{ pkgs, lib, ... }:

{
# Runtime
virtualisation.podman = {
enable = true;
autoPrune.enable = true;
dockerCompat = true;
defaultNetwork.settings = {
# Required for container networking to be able to use names.
dns_enabled = true;
};
};
virtualisation.oci-containers.backend = "podman";

# Containers
virtualisation.oci-containers.containers."test-all" = {
image = "nginx:latest";
cmd = [ "ls" "-la" ];
log-driver = "journald";
autoStart = false;
extraOptions = [
"--entrypoint=["nginx", "-g", "daemon off;", "-c", "/etc/config/nginx/conf/nginx.conf"]"
"--network-alias=all"
"--network=test_default"
];
};
systemd.services."podman-test-all" = {
serviceConfig = {
Restart = lib.mkOverride 90 "no";
};
after = [
"podman-network-test_default.service"
];
requires = [
"podman-network-test_default.service"
];
};
virtualisation.oci-containers.containers."test-empty-command" = {
image = "nginx:latest";
cmd = [ ];
log-driver = "journald";
autoStart = false;
extraOptions = [
"--network-alias=empty-command"
"--network=test_default"
];
};
systemd.services."podman-test-empty-command" = {
serviceConfig = {
Restart = lib.mkOverride 90 "no";
};
after = [
"podman-network-test_default.service"
];
requires = [
"podman-network-test_default.service"
];
};
virtualisation.oci-containers.containers."test-empty-entrypoint" = {
image = "nginx:latest";
log-driver = "journald";
autoStart = false;
extraOptions = [
"--entrypoint=[]"
"--network-alias=empty-entrypoint"
"--network=test_default"
];
};
systemd.services."podman-test-empty-entrypoint" = {
serviceConfig = {
Restart = lib.mkOverride 90 "no";
};
after = [
"podman-network-test_default.service"
];
requires = [
"podman-network-test_default.service"
];
};
# Networks
systemd.services."podman-network-test_default" = {
path = [ pkgs.podman ];
serviceConfig = {
Type = "oneshot";
RemainAfterExit = true;
ExecStop = "podman network rm -f test_default";
};
script = ''
podman network inspect test_default || podman network create test_default
'';
partOf = [ "podman-compose-test-root.target" ];
wantedBy = [ "podman-compose-test-root.target" ];
};
# Root service
# When started, this will automatically create all resources and start
# the containers. When stopped, this will teardown all resources.
systemd.targets."podman-compose-test-root" = {
unitConfig = {
Description = "Root target generated by compose2nix.";
};
};
}

0 comments on commit 7689b33

Please sign in to comment.