From a8d9725e6ccf0e3e9da8bcbeea89657a8273d4c2 Mon Sep 17 00:00:00 2001 From: LoveIsGrief Date: Sat, 19 Aug 2023 22:11:17 +0200 Subject: [PATCH 1/3] chore: Add *.swp to .gitignore When working with vim (and possibly other editors), swap files are created and they should be ignored. --- .gitignore | 2 ++ 1 file changed, 2 insertions(+) diff --git a/.gitignore b/.gitignore index 49eb116..faece1c 100644 --- a/.gitignore +++ b/.gitignore @@ -5,3 +5,5 @@ dist/ dist-newstyle/ cabal.project.local +*.swp + From 638c4b8e552a86c315acbe32fd6cbd399885d1d0 Mon Sep 17 00:00:00 2001 From: LoveIsGrief Date: Sat, 19 Aug 2023 22:13:18 +0200 Subject: [PATCH 2/3] fix: services..service.build.context One could declare it, but it was unceremoniously ignored. A "localhost/" image was created with pretty much nothing in it and it couldn't be launched. The cause was services..service.image being always set and subsequently thugs services..image.nixBuild being truthy. That would build an image and write the services..image field in the docker-compose.json. It leads to the build.context being ignored and the service failing. This was solved by only writing services..service.image when services..service.build.context is not set. services..image.nixBuild is additionally set to false when the context is set. Related to #208 --- .../service/docker-compose-service.nix | 6 ++-- src/nix/modules/service/image.nix | 28 ++++++++++--------- tests/arion-test/default.nix | 2 +- 3 files changed, 20 insertions(+), 16 deletions(-) diff --git a/src/nix/modules/service/docker-compose-service.nix b/src/nix/modules/service/docker-compose-service.nix index 061ae8b..c038ac6 100644 --- a/src/nix/modules/service/docker-compose-service.nix +++ b/src/nix/modules/service/docker-compose-service.nix @@ -86,7 +86,8 @@ in description = serviceRef "environment"; }; service.image = mkOption { - type = str; + type = nullOr str; + default = null; description = serviceRef "image"; }; service.command = mkOption { @@ -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 != []) { diff --git a/src/nix/modules/service/image.nix b/src/nix/modules/service/image.nix index 706a5c1..ae5d214 100644 --- a/src/nix/modules/service/image.nix +++ b/src/nix/modules/service/image.nix @@ -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}"; + }) + ]; } diff --git a/tests/arion-test/default.nix b/tests/arion-test/default.nix index 75f940d..f8ba4c3 100644 --- a/tests/arion-test/default.nix +++ b/tests/arion-test/default.nix @@ -29,7 +29,7 @@ in enable = true; dockerSocket.enable = true; }; - + # no caches, because no internet nix.settings.substituters = lib.mkForce []; From 3588b01e13b737417a1d3e38d34876a65b1bbcb6 Mon Sep 17 00:00:00 2001 From: LoveIsGrief Date: Sun, 20 Aug 2023 15:22:40 +0200 Subject: [PATCH 3/3] test: Add tests for using build.context These are simple tests to make sure that the generated docker-compose.json looks like it should. This means has a build.context and does NOT have an image defined - the image is built when starting the service. --- src/haskell/test/Arion/NixSpec.hs | 41 +++++++++++++------ src/haskell/test/Spec.hs | 1 + .../Arion/NixSpec/arion-context-compose.json | 41 +++++++++++++++++++ .../Arion/NixSpec/arion-context-compose.nix | 9 ++++ .../Arion/NixSpec/build-context/Dockerfile | 4 ++ 5 files changed, 83 insertions(+), 13 deletions(-) create mode 100644 src/haskell/testdata/Arion/NixSpec/arion-context-compose.json create mode 100644 src/haskell/testdata/Arion/NixSpec/arion-context-compose.nix create mode 100644 src/haskell/testdata/Arion/NixSpec/build-context/Dockerfile diff --git a/src/haskell/test/Arion/NixSpec.hs b/src/haskell/test/Arion/NixSpec.hs index e20bebf..8f0c25e 100644 --- a/src/haskell/test/Arion/NixSpec.hs +++ b/src/haskell/test/Arion/NixSpec.hs @@ -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 { 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 { 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 { 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 diff --git a/src/haskell/test/Spec.hs b/src/haskell/test/Spec.hs index d2da234..06d3acc 100644 --- a/src/haskell/test/Spec.hs +++ b/src/haskell/test/Spec.hs @@ -9,3 +9,4 @@ import qualified Arion.NixSpec spec :: Spec spec = do describe "Arion.Nix" Arion.NixSpec.spec + diff --git a/src/haskell/testdata/Arion/NixSpec/arion-context-compose.json b/src/haskell/testdata/Arion/NixSpec/arion-context-compose.json new file mode 100644 index 0000000..ae67f53 --- /dev/null +++ b/src/haskell/testdata/Arion/NixSpec/arion-context-compose.json @@ -0,0 +1,41 @@ +{ + "networks": { + "default": { + "name": "unit-test-data" + } + }, + "services": { + "webserver": { + "build": { + "context": "" + }, + "environment": {}, + "ports": [ + "8080:80" + ], + "sysctls": {}, + "volumes": [] + } + }, + "version": "3.4", + "volumes": {}, + "x-arion": { + "images": [ + { + "imageExe": "", + "imageName": "localhost/webserver", + "imageTag": "" + } + ], + "project": { + "name": "unit-test-data" + }, + "serviceInfo": { + "webserver": { + "defaultExec": [ + "/bin/sh" + ] + } + } + } +} diff --git a/src/haskell/testdata/Arion/NixSpec/arion-context-compose.nix b/src/haskell/testdata/Arion/NixSpec/arion-context-compose.nix new file mode 100644 index 0000000..a4c5b7c --- /dev/null +++ b/src/haskell/testdata/Arion/NixSpec/arion-context-compose.nix @@ -0,0 +1,9 @@ +{ + project.name = "unit-test-data"; + services.webserver.service = { + build.context = "${./build-context}"; + ports = [ + "8080:80" + ]; + }; +} diff --git a/src/haskell/testdata/Arion/NixSpec/build-context/Dockerfile b/src/haskell/testdata/Arion/NixSpec/build-context/Dockerfile new file mode 100644 index 0000000..d2ba487 --- /dev/null +++ b/src/haskell/testdata/Arion/NixSpec/build-context/Dockerfile @@ -0,0 +1,4 @@ +FROM nginx + +RUN echo this is a dockerfile to be built +