Skip to content

Commit

Permalink
modify oci tmpl for layering
Browse files Browse the repository at this point in the history
Signed-off-by: Rakshit Gondwal <[email protected]>
  • Loading branch information
rakshitgondwal committed Aug 17, 2024
1 parent 65919e6 commit 91c2e4b
Show file tree
Hide file tree
Showing 2 changed files with 77 additions and 64 deletions.
119 changes: 55 additions & 64 deletions pkg/nix/template/oci.go
Original file line number Diff line number Diff line change
Expand Up @@ -17,11 +17,10 @@ type OCIArtifact struct {
ImportConfigs []string
ExposedPorts []string
Base bool
Layer *string
}

const (
ociTmpl = `
ociTmpl = `
ociImages = forEachSupportedSystem ({ pkgs, nix2containerPkgs, system , ...}: {
{{range $artifact := .}}
{{ if ne ($artifact.Base) true }}
Expand All @@ -44,15 +43,18 @@ const (
};
};
maxLayers = 100;
layers = [
(nix2containerPkgs.nix2container.buildLayer {
copyToRoot = [
inputs.self.runtimeEnvs.${system}.runtime
{{range $config := $artifact.ImportConfigs}}
inputs.self.configs.${system}.config_{{ . }} {{end}}
];
})
];
layers = builtins.concatLists [
(map (pkg: nix2containerPkgs.nix2container.buildLayer {
copyToRoot = [ pkg ];
}) inputs.self.runtimeEnvsForOCI.${system}.runtime)
{{range $config := $artifact.ImportConfigs}}
(nix2containerPkgs.nix2container.buildLayer {
copyToRoot = [ inputs.self.configs.${system}.config_{{ . }} ];
}),
{{end}}
];
};
ociImage_{{$artifact.Artifact}}_app_with_dev = nix2containerPkgs.nix2container.buildImage {
Expand All @@ -74,16 +76,21 @@ const (
};
};
maxLayers = 100;
layers = [
(nix2containerPkgs.nix2container.buildLayer {
copyToRoot = [
inputs.self.runtimeEnvs.${system}.runtime
{{range $config := $artifact.ImportConfigs}}
inputs.self.configs.${system}.config_{{ . }} {{end}}
inputs.self.devEnvs.${system}.development
];
})
];
layers = builtins.concatLists [
(map (pkg: nix2containerPkgs.nix2container.buildLayer {
copyToRoot = [ pkg ];
}) inputs.self.runtimeEnvsForOCI.${system}.runtime)
{{range $config := $artifact.ImportConfigs}}
(nix2containerPkgs.nix2container.buildLayer {
copyToRoot = [ inputs.self.configs.${system}.config_{{ . }} ];
}),
{{end}}
(map (pkg: nix2containerPkgs.nix2container.buildLayer {
copyToRoot = [ pkg ];
}) inputs.self.devEnvsForOCI.${system}.development)
];
};
{{end}}
Expand All @@ -106,22 +113,18 @@ const (
};
};
maxLayers = 100;
layers = [
{{range $runtime := inputs.self.runtimeEnvs.${system}.runtime}}
(nix2containerPkgs.nix2container.buildLayer {
copyToRoot = [
{{ . }}
];
})
{{end}}
layers = builtins.concatLists [
(map (pkg: nix2containerPkgs.nix2container.buildLayer {
copyToRoot = [ pkg ];
}) inputs.self.runtimeEnvsForOCI.${system}.runtime)
{{range $config := $artifact.ImportConfigs}}
(nix2containerPkgs.nix2container.buildLayer {
copyToRoot = [
inputs.self.configs.${system}.config_{{ . }}
];
})
(nix2containerPkgs.nix2container.buildLayer {
copyToRoot = [ inputs.self.configs.${system}.config_{{ . }} ];
}),
{{end}}
];
];
};
ociImage_{{$artifact.Artifact}}_dev = nix2containerPkgs.nix2container.buildImage {
Expand All @@ -142,16 +145,21 @@ const (
};
};
maxLayers = 100;
layers = [
(nix2containerPkgs.nix2container.buildLayer {
copyToRoot = [
inputs.self.runtimeEnvs.${system}.runtime
{{range $config := $artifact.ImportConfigs}}
inputs.self.configs.${system}.config_{{ . }} {{end}}
inputs.self.devEnvs.${system}.development
];
})
];
layers = builtins.concatLists [
(map (pkg: nix2containerPkgs.nix2container.buildLayer {
copyToRoot = [ pkg ];
}) inputs.self.runtimeEnvsForOCI.${system}.runtime)
{{range $config := $artifact.ImportConfigs}}
(nix2containerPkgs.nix2container.buildLayer {
copyToRoot = [ inputs.self.configs.${system}.config_{{ . }} ];
}),
{{end}}
(map (pkg: nix2containerPkgs.nix2container.buildLayer {
copyToRoot = [ pkg ];
}) inputs.self.devEnvsForOCI.${system}.development)
];
};
{{end}}
{{end}}
Expand All @@ -168,20 +176,9 @@ const (
{{end}}
});
`

runtimeLayer = `
layers = [
(nix2containerPkgs.nix2container.buildLayer {
copyToRoot = [
inputs.self.runtimeEnvs.${system}.runtime
{{range $config := $artifact.ImportConfigs}}
inputs.self.configs.${system}.config_{{ . }} {{end}}
];
})
];
`
)


func hclOCIToOCIArtifact(ociArtifacts []hcl2nix.OCIArtifact) []OCIArtifact {
converted := make([]OCIArtifact, len(ociArtifacts))

Expand All @@ -195,11 +192,9 @@ func hclOCIToOCIArtifact(ociArtifacts []hcl2nix.OCIArtifact) []OCIArtifact {
ImportConfigs: ociArtifact.ImportConfigs,
ExposedPorts: ociArtifact.ExposedPorts,
}
// converted[i].Layer = getLayer(ociArtifact.Layers)
if ociArtifact.IsBase {
converted[i].Base = true
}

}
return converted
}
Expand All @@ -222,8 +217,4 @@ func GenerateOCIAttr(artifacts []OCIArtifact) (*string, error) {

result := buf.String()
return &result, nil
}

// func getLayer([]string) *string {

// }
}
22 changes: 22 additions & 0 deletions pkg/nix/template/templates.go
Original file line number Diff line number Diff line change
Expand Up @@ -165,6 +165,28 @@ const (
];
};
});
runtimeEnvsForOCI = forEachSupportedSystem ({ pkgs,
{{if eq .Language "GoModule"}} buildGoApplication, {{end}}
{{if eq .Language "PythonPoetry"}} mkPoetryApplication, {{end}}
{{ if eq .Language "JsNpm"}} buildNodeModules, {{end}}
{{ range .NixPackageRevisions }} nixpkgs-{{ .}}-pkgs, {{ end }} ... }: {
runtime = [
{{ range $key, $value := .RuntimePackages }}nixpkgs-{{ $value }}-pkgs.{{$key}}
{{ end }}
];
});
devEnvsForOCI = forEachSupportedSystem ({ pkgs,
{{if eq .Language "GoModule"}} buildGoApplication, {{end}}
{{if eq .Language "PythonPoetry"}} mkPoetryApplication, {{end}}
{{ if eq .Language "JsNpm"}} buildNodeModules, {{end}}
{{ range .NixPackageRevisions }} nixpkgs-{{ .}}-pkgs, {{ end }} ... }: {
development = [
{{ range $key, $value :=.DevPackages }}nixpkgs-{{ $value }}-pkgs.{{ $key }}
{{ end }}
];
});
{{if .ConfigAttribute}}
{{.ConfigAttribute}}
Expand Down

0 comments on commit 91c2e4b

Please sign in to comment.