209: fix: services.<name>.service.build.context r=roberth a=LoveIsGrief

 - [x] Support services.<name>.service.build.context see 638c4b8  for more details
 - [x] Add test

Closes #208

Co-authored-by: LoveIsGrief <loveisgrief@tuta.io>
This commit is contained in:
bors[bot] 2023-08-20 16:16:39 +00:00 committed by GitHub
commit 51ed7054c1
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
9 changed files with 105 additions and 29 deletions

2
.gitignore vendored
View file

@ -5,3 +5,5 @@ dist/
dist-newstyle/ dist-newstyle/
cabal.project.local cabal.project.local
*.swp

View file

@ -13,7 +13,8 @@ import qualified Data.Text as T
import qualified Data.Text.IO as T import qualified Data.Text.IO as T
spec :: Spec spec :: Spec
spec = describe "evaluateComposition" $ it "matches an example" $ do spec = describe "evaluateComposition" $ do
it "matches an example" $ do
x <- Arion.Nix.evaluateComposition EvaluationArgs x <- Arion.Nix.evaluateComposition EvaluationArgs
{ evalUid = 123 { evalUid = 123
, evalModules = NEL.fromList , evalModules = NEL.fromList
@ -27,6 +28,20 @@ spec = describe "evaluateComposition" $ it "matches an example" $ do
expected <- T.readFile "src/haskell/testdata/Arion/NixSpec/arion-compose.json" expected <- T.readFile "src/haskell/testdata/Arion/NixSpec/arion-compose.json"
censorPaths actual `shouldBe` censorPaths expected censorPaths actual `shouldBe` censorPaths expected
it "matches an build.context example" $ do
x <- Arion.Nix.evaluateComposition EvaluationArgs
{ evalUid = 1234
, evalModules = NEL.fromList
["src/haskell/testdata/Arion/NixSpec/arion-context-compose.nix"]
, evalPkgs = "import <nixpkgs> { system = \"x86_64-linux\"; }"
, evalWorkDir = Nothing
, evalMode = ReadOnly
, evalUserArgs = ["--show-trace"]
}
let actual = pretty x
expected <- T.readFile "src/haskell/testdata/Arion/NixSpec/arion-context-compose.json"
censorPaths actual `shouldBe` censorPaths expected
censorPaths :: Text -> Text censorPaths :: Text -> Text
censorPaths = censorImages . censorStorePaths censorPaths = censorImages . censorStorePaths

View file

@ -9,3 +9,4 @@ import qualified Arion.NixSpec
spec :: Spec spec :: Spec
spec = do spec = do
describe "Arion.Nix" Arion.NixSpec.spec describe "Arion.Nix" Arion.NixSpec.spec

View file

@ -0,0 +1,41 @@
{
"networks": {
"default": {
"name": "unit-test-data"
}
},
"services": {
"webserver": {
"build": {
"context": "<STOREPATH>"
},
"environment": {},
"ports": [
"8080:80"
],
"sysctls": {},
"volumes": []
}
},
"version": "3.4",
"volumes": {},
"x-arion": {
"images": [
{
"imageExe": "<STOREPATH>",
"imageName": "localhost/webserver",
"imageTag": "<HASH>"
}
],
"project": {
"name": "unit-test-data"
},
"serviceInfo": {
"webserver": {
"defaultExec": [
"/bin/sh"
]
}
}
}
}

View file

@ -0,0 +1,9 @@
{
project.name = "unit-test-data";
services.webserver.service = {
build.context = "${./build-context}";
ports = [
"8080:80"
];
};
}

View file

@ -0,0 +1,4 @@
FROM nginx
RUN echo this is a dockerfile to be built

View file

@ -86,7 +86,8 @@ in
description = serviceRef "environment"; description = serviceRef "environment";
}; };
service.image = mkOption { service.image = mkOption {
type = str; type = nullOr str;
default = null;
description = serviceRef "image"; description = serviceRef "image";
}; };
service.command = mkOption { service.command = mkOption {
@ -328,8 +329,9 @@ in
volumes volumes
environment environment
sysctls sysctls
image
; ;
} // lib.optionalAttrs (config.service.image != null) {
inherit (config.service) image;
} // lib.optionalAttrs (config.service.build.context != null) { } // lib.optionalAttrs (config.service.build.context != null) {
inherit (config.service) build; inherit (config.service) build;
} // lib.optionalAttrs (cap_add != []) { } // lib.optionalAttrs (cap_add != []) {

View file

@ -163,17 +163,19 @@ in
''; '';
}; };
}; };
config = { config = lib.mkMerge [{
build.image = builtImage; build.image = builtImage;
build.imageName = config.build.image.imageName; build.imageName = config.build.image.imageName;
build.imageTag = build.imageTag =
if config.build.image.imageTag != "" if config.build.image.imageTag != ""
then config.build.image.imageTag then config.build.image.imageTag
else lib.head (lib.strings.splitString "-" (baseNameOf config.build.image.outPath)); else lib.head (lib.strings.splitString "-" (baseNameOf config.build.image.outPath));
service.image = lib.mkDefault "${config.build.imageName}:${config.build.imageTag}";
image.rawConfig.Cmd = config.image.command; image.rawConfig.Cmd = config.image.command;
image.nixBuild = lib.mkDefault (priorityIsDefault options.service.image); image.nixBuild = lib.mkDefault (priorityIsDefault options.service.image);
}; }
( lib.mkIf (config.service.build.context == null)
{
service.image = lib.mkDefault "${config.build.imageName}:${config.build.imageTag}";
})
];
} }