Skip to content

Commit

Permalink
fix: use built-in Git support in build
Browse files Browse the repository at this point in the history
  • Loading branch information
aksiksi committed Sep 29, 2024
1 parent 6df404d commit f9f21f2
Show file tree
Hide file tree
Showing 7 changed files with 23 additions and 26 deletions.
2 changes: 1 addition & 1 deletion compose.go
Original file line number Diff line number Diff line change
Expand Up @@ -738,7 +738,7 @@ func (g *Generator) parseServiceBuild(service types.ServiceConfig, c *NixContain
// the image to it.
imageName := fmt.Sprintf("compose2nix-%s", c.Name)
tags = append(tags, imageName)
c.Image = imageName
c.Image = fmt.Sprintf("localhost/%s", imageName)
}

b := &NixBuild{
Expand Down
9 changes: 8 additions & 1 deletion nix.go
Original file line number Diff line number Diff line change
Expand Up @@ -197,6 +197,7 @@ func (c *NixContainer) Unit() string {
}

// https://docs.docker.com/reference/compose-file/services/#pull_policy
// https://docs.podman.io/en/latest/markdown/podman-build.1.html#pull-policy
type ServicePullPolicy int

const (
Expand Down Expand Up @@ -261,7 +262,13 @@ func (b *NixBuild) Command() string {
cmd += fmt.Sprintf(" -f %s", b.Dockerfile)
}

return cmd + " ."
if b.IsGitRepo {
cmd += " " + b.Context
} else {
cmd += " ."
}

return cmd
}

type NixContainerConfig struct {
Expand Down
8 changes: 2 additions & 6 deletions templates/build.nix.tmpl
Original file line number Diff line number Diff line change
Expand Up @@ -3,17 +3,13 @@ systemd.services."{{.UnitName}}" = {
path = [ pkgs.{{.Runtime}} pkgs.git ];
serviceConfig = {
Type = "oneshot";
RuntimeDirectory = "{{.UnitName}}";
{{- if cfg.IncludeBuild}}
RemainAfterExit = true;
{{- end}}
TimeoutSec = 300;
};
script = ''
{{- if .IsGitRepo}}
cd /var/run/{{.UnitName}}
rm -rf *
git clone {{.Context}} .
{{- else}}
{{- if not .IsGitRepo}}
cd {{.Context}}
{{- end}}
{{escapeIndentedNixString .Command}}
Expand Down
11 changes: 4 additions & 7 deletions testdata/TestBuildSpec.docker.nix
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@

# Containers
virtualisation.oci-containers.containers."test-museum" = {
image = "compose2nix-test-museum";
image = "localhost/compose2nix-test-museum";
environment = {
"ENTE_CREDENTIALS_FILE" = "/credentials.yaml";
};
Expand Down Expand Up @@ -118,7 +118,7 @@
path = [ pkgs.docker pkgs.git ];
serviceConfig = {
Type = "oneshot";
RuntimeDirectory = "docker-build-test-museum";
TimeoutSec = 300;
};
script = ''
cd /some/path
Expand All @@ -129,13 +129,10 @@
path = [ pkgs.docker pkgs.git ];
serviceConfig = {
Type = "oneshot";
RuntimeDirectory = "docker-build-test-prefetcharr";
TimeoutSec = 300;
};
script = ''
cd /var/run/docker-build-test-prefetcharr
rm -rf *
git clone https://github.com/p-hueber/prefetcharr.git .
docker build -t prefetcharr .
docker build -t prefetcharr https://github.com/p-hueber/prefetcharr.git
'';
};

Expand Down
11 changes: 4 additions & 7 deletions testdata/TestBuildSpec.podman.nix
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@

# Containers
virtualisation.oci-containers.containers."test-museum" = {
image = "compose2nix-test-museum";
image = "localhost/compose2nix-test-museum";
environment = {
"ENTE_CREDENTIALS_FILE" = "/credentials.yaml";
};
Expand Down Expand Up @@ -128,7 +128,7 @@
path = [ pkgs.podman pkgs.git ];
serviceConfig = {
Type = "oneshot";
RuntimeDirectory = "podman-build-test-museum";
TimeoutSec = 300;
};
script = ''
cd /some/path
Expand All @@ -139,13 +139,10 @@
path = [ pkgs.podman pkgs.git ];
serviceConfig = {
Type = "oneshot";
RuntimeDirectory = "podman-build-test-prefetcharr";
TimeoutSec = 300;
};
script = ''
cd /var/run/podman-build-test-prefetcharr
rm -rf *
git clone https://github.com/p-hueber/prefetcharr.git .
podman build -t prefetcharr .
podman build -t prefetcharr https://github.com/p-hueber/prefetcharr.git
'';
};

Expand Down
4 changes: 2 additions & 2 deletions testdata/TestBuildSpec_BuildEnabled.docker.nix
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@

# Containers
virtualisation.oci-containers.containers."test-museum" = {
image = "compose2nix-test-museum";
image = "localhost/compose2nix-test-museum";
environment = {
"ENTE_CREDENTIALS_FILE" = "/credentials.yaml";
};
Expand Down Expand Up @@ -84,8 +84,8 @@
path = [ pkgs.docker pkgs.git ];
serviceConfig = {
Type = "oneshot";
RuntimeDirectory = "docker-build-test-museum";
RemainAfterExit = true;
TimeoutSec = 300;
};
script = ''
cd /some/path
Expand Down
4 changes: 2 additions & 2 deletions testdata/TestBuildSpec_BuildEnabled.podman.nix
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@

# Containers
virtualisation.oci-containers.containers."test-museum" = {
image = "compose2nix-test-museum";
image = "localhost/compose2nix-test-museum";
environment = {
"ENTE_CREDENTIALS_FILE" = "/credentials.yaml";
};
Expand Down Expand Up @@ -94,8 +94,8 @@
path = [ pkgs.podman pkgs.git ];
serviceConfig = {
Type = "oneshot";
RuntimeDirectory = "podman-build-test-museum";
RemainAfterExit = true;
TimeoutSec = 300;
};
script = ''
cd /some/path
Expand Down

0 comments on commit f9f21f2

Please sign in to comment.