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 b51cc55
Show file tree
Hide file tree
Showing 6 changed files with 16 additions and 26 deletions.
11 changes: 9 additions & 2 deletions 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 @@ -245,7 +246,7 @@ func (b *NixBuild) Unit() string {
}

func (b *NixBuild) Command() string {
cmd := fmt.Sprintf("%s build", b.Runtime)
cmd := fmt.Sprintf("sudo %s build", b.Runtime)

for _, tag := range b.Tags {
cmd += fmt.Sprintf(" -t %s", tag)
Expand All @@ -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
7 changes: 1 addition & 6 deletions templates/build.nix.tmpl
Original file line number Diff line number Diff line change
Expand Up @@ -3,17 +3,12 @@ systemd.services."{{.UnitName}}" = {
path = [ pkgs.{{.Runtime}} pkgs.git ];
serviceConfig = {
Type = "oneshot";
RuntimeDirectory = "{{.UnitName}}";
{{- if cfg.IncludeBuild}}
RemainAfterExit = true;
{{- end}}
};
script = ''
{{- if .IsGitRepo}}
cd /var/run/{{.UnitName}}
rm -rf *
git clone {{.Context}} .
{{- else}}
{{- if not .IsGitRepo}}
cd {{.Context}}
{{- end}}
{{escapeIndentedNixString .Command}}
Expand Down
9 changes: 2 additions & 7 deletions testdata/TestBuildSpec.docker.nix
Original file line number Diff line number Diff line change
Expand Up @@ -118,24 +118,19 @@
path = [ pkgs.docker pkgs.git ];
serviceConfig = {
Type = "oneshot";
RuntimeDirectory = "docker-build-test-museum";
};
script = ''
cd /some/path
docker build -t latest -t non-latest -t compose2nix-test-museum --build-arg GIT_COMMIT=development-cluster .
sudo docker build -t latest -t non-latest -t compose2nix-test-museum --build-arg GIT_COMMIT=development-cluster .
'';
};
systemd.services."docker-build-test-prefetcharr" = {
path = [ pkgs.docker pkgs.git ];
serviceConfig = {
Type = "oneshot";
RuntimeDirectory = "docker-build-test-prefetcharr";
};
script = ''
cd /var/run/docker-build-test-prefetcharr
rm -rf *
git clone https://github.com/p-hueber/prefetcharr.git .
docker build -t prefetcharr .
sudo docker build -t prefetcharr https://github.com/p-hueber/prefetcharr.git
'';
};

Expand Down
9 changes: 2 additions & 7 deletions testdata/TestBuildSpec.podman.nix
Original file line number Diff line number Diff line change
Expand Up @@ -128,24 +128,19 @@
path = [ pkgs.podman pkgs.git ];
serviceConfig = {
Type = "oneshot";
RuntimeDirectory = "podman-build-test-museum";
};
script = ''
cd /some/path
podman build -t latest -t non-latest -t compose2nix-test-museum --build-arg GIT_COMMIT=development-cluster .
sudo podman build -t latest -t non-latest -t compose2nix-test-museum --build-arg GIT_COMMIT=development-cluster .
'';
};
systemd.services."podman-build-test-prefetcharr" = {
path = [ pkgs.podman pkgs.git ];
serviceConfig = {
Type = "oneshot";
RuntimeDirectory = "podman-build-test-prefetcharr";
};
script = ''
cd /var/run/podman-build-test-prefetcharr
rm -rf *
git clone https://github.com/p-hueber/prefetcharr.git .
podman build -t prefetcharr .
sudo podman build -t prefetcharr https://github.com/p-hueber/prefetcharr.git
'';
};

Expand Down
3 changes: 1 addition & 2 deletions testdata/TestBuildSpec_BuildEnabled.docker.nix
Original file line number Diff line number Diff line change
Expand Up @@ -84,12 +84,11 @@
path = [ pkgs.docker pkgs.git ];
serviceConfig = {
Type = "oneshot";
RuntimeDirectory = "docker-build-test-museum";
RemainAfterExit = true;
};
script = ''
cd /some/path
docker build -t compose2nix-test-museum --build-arg GIT_COMMIT=development-cluster -f path/Dockerfile .
sudo docker build -t compose2nix-test-museum --build-arg GIT_COMMIT=development-cluster -f path/Dockerfile .
'';
partOf = [ "docker-compose-test-root.target" ];
wantedBy = [ "docker-compose-test-root.target" ];
Expand Down
3 changes: 1 addition & 2 deletions testdata/TestBuildSpec_BuildEnabled.podman.nix
Original file line number Diff line number Diff line change
Expand Up @@ -94,12 +94,11 @@
path = [ pkgs.podman pkgs.git ];
serviceConfig = {
Type = "oneshot";
RuntimeDirectory = "podman-build-test-museum";
RemainAfterExit = true;
};
script = ''
cd /some/path
podman build -t compose2nix-test-museum --build-arg GIT_COMMIT=development-cluster -f path/Dockerfile .
sudo podman build -t compose2nix-test-museum --build-arg GIT_COMMIT=development-cluster -f path/Dockerfile .
'';
partOf = [ "podman-compose-test-root.target" ];
wantedBy = [ "podman-compose-test-root.target" ];
Expand Down

0 comments on commit b51cc55

Please sign in to comment.