Skip to content

Commit

Permalink
Merge #209
Browse files Browse the repository at this point in the history
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 <[email protected]>
  • Loading branch information
bors[bot] and LoveIsGrief authored Aug 20, 2023
2 parents f0436c8 + 3588b01 commit 51ed705
Show file tree
Hide file tree
Showing 9 changed files with 105 additions and 29 deletions.
2 changes: 2 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -5,3 +5,5 @@ dist/
dist-newstyle/
cabal.project.local

*.swp

41 changes: 28 additions & 13 deletions src/haskell/test/Arion/NixSpec.hs
Original file line number Diff line number Diff line change
Expand Up @@ -13,19 +13,34 @@ import qualified Data.Text as T
import qualified Data.Text.IO as T

spec :: Spec
spec = describe "evaluateComposition" $ it "matches an example" $ do
x <- Arion.Nix.evaluateComposition EvaluationArgs
{ evalUid = 123
, evalModules = NEL.fromList
["src/haskell/testdata/Arion/NixSpec/arion-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-compose.json"
censorPaths actual `shouldBe` censorPaths expected
spec = describe "evaluateComposition" $ do
it "matches an example" $ do
x <- Arion.Nix.evaluateComposition EvaluationArgs
{ evalUid = 123
, evalModules = NEL.fromList
["src/haskell/testdata/Arion/NixSpec/arion-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-compose.json"
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 = censorImages . censorStorePaths
Expand Down
1 change: 1 addition & 0 deletions src/haskell/test/Spec.hs
Original file line number Diff line number Diff line change
Expand Up @@ -9,3 +9,4 @@ import qualified Arion.NixSpec
spec :: Spec
spec = do
describe "Arion.Nix" Arion.NixSpec.spec

41 changes: 41 additions & 0 deletions src/haskell/testdata/Arion/NixSpec/arion-context-compose.json
Original file line number Diff line number Diff line change
@@ -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"
]
}
}
}
}
9 changes: 9 additions & 0 deletions src/haskell/testdata/Arion/NixSpec/arion-context-compose.nix
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
{
project.name = "unit-test-data";
services.webserver.service = {
build.context = "${./build-context}";
ports = [
"8080:80"
];
};
}
4 changes: 4 additions & 0 deletions src/haskell/testdata/Arion/NixSpec/build-context/Dockerfile
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
FROM nginx

RUN echo this is a dockerfile to be built

6 changes: 4 additions & 2 deletions src/nix/modules/service/docker-compose-service.nix
Original file line number Diff line number Diff line change
Expand Up @@ -86,7 +86,8 @@ in
description = serviceRef "environment";
};
service.image = mkOption {
type = str;
type = nullOr str;
default = null;
description = serviceRef "image";
};
service.command = mkOption {
Expand Down Expand Up @@ -328,8 +329,9 @@ in
volumes
environment
sysctls
image
;
} // lib.optionalAttrs (config.service.image != null) {
inherit (config.service) image;
} // lib.optionalAttrs (config.service.build.context != null) {
inherit (config.service) build;
} // lib.optionalAttrs (cap_add != []) {
Expand Down
28 changes: 15 additions & 13 deletions src/nix/modules/service/image.nix
Original file line number Diff line number Diff line change
Expand Up @@ -163,17 +163,19 @@ in
'';
};
};
config = {
build.image = builtImage;
build.imageName = config.build.image.imageName;
build.imageTag =
if config.build.image.imageTag != ""
then config.build.image.imageTag
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.nixBuild = lib.mkDefault (priorityIsDefault options.service.image);
};
config = lib.mkMerge [{
build.image = builtImage;
build.imageName = config.build.image.imageName;
build.imageTag =
if config.build.image.imageTag != ""
then config.build.image.imageTag
else lib.head (lib.strings.splitString "-" (baseNameOf config.build.image.outPath));
image.rawConfig.Cmd = config.image.command;
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}";
})
];
}
2 changes: 1 addition & 1 deletion tests/arion-test/default.nix
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,7 @@ in
enable = true;
dockerSocket.enable = true;
};

# no caches, because no internet
nix.settings.substituters = lib.mkForce [];

Expand Down

0 comments on commit 51ed705

Please sign in to comment.