Skip to content

Commit

Permalink
import attrset via runCommand
Browse files Browse the repository at this point in the history
  • Loading branch information
aksiksi committed Nov 8, 2023
1 parent f681ae3 commit 4e6ac64
Show file tree
Hide file tree
Showing 6 changed files with 34 additions and 27 deletions.
51 changes: 34 additions & 17 deletions flake.nix
Original file line number Diff line number Diff line change
Expand Up @@ -42,9 +42,13 @@
in {
options.compose2nix = {
# https://nixos.org/manual/nixos/stable/#sec-option-declarations
enable = mkOption {
type = types.bool;
default = false;
description = lib.mdDoc "Enable compose2nix.";
};
paths = mkOption {
type = types.listOf types.path;
default = [];
description = lib.mdDoc "One or more paths to Docker Compose files.";
};
runtime = mkOption {
Expand Down Expand Up @@ -87,27 +91,40 @@
default = true;
description = lib.mdDoc "Auto-start all containers.";
};
output = mkOption {
type = types.anything;
description = lib.mdDoc "Output config.";
};
};
config.compose2nix = mkIf (cfg.paths != []) {
# runCommandLocal ensures that we always build this derivation on the local machine.
# This allows us to circumvent the Nix binary cache and minimize the time spent outside
# of building the derivation.
# https://nixos.org/manual/nixpkgs/stable/#trivial-builder-runCommandLocal
output = pkgs.runCommandLocal "run-compose2nix" {
env = cfg.env;
# runCommandLocal ensures that we always build this derivation on the local machine.
# This allows us to circumvent the Nix binary cache and minimize the time spent outside
# of building the derivation.
# https://nixos.org/manual/nixpkgs/stable/#trivial-builder-runCommandLocal
config = let
env = cfg.env;
paths = cfg.paths;
runtime = cfg.runtime;
project = cfg.project;
projectSeparator = cfg.projectSeparator;
envFilesOnly = cfg.envFilesOnly;
serviceInclude = cfg.serviceInclude;
autoStart = cfg.autoStart;
in {
config.compose2nix.output = mkIf (paths != []) import (pkgs.runCommandLocal "compose2nix" {
env = env;
buildInputs = [ pkgs.compose2nix ];
} ''
${pkgs.compose2nix}/bin/compose2nix \
-paths='${concatStringsSep "," cfg.paths}' \
-runtime=${cfg.runtime} \
-project=${cfg.project} \
-project_separator='${cfg.projectSeparator}' \
-env_files='${concatStringsSep "," cfg.envFiles}' \
-env_files_only=${cfg.envFilesOnly} \
-service_include='${cfg.serviceInclude}' \
-auto_start=${cfg.autoStart} \
-paths='${concatStringsSep "," paths}' \
-runtime=${runtime} \
-project=${project} \
-project_separator='${projectSeparator}' \
-env_files='${concatStringsSep "," envFiles}' \
-env_files_only=${envFilesOnly} \
-service_include='${serviceInclude}' \
-auto_start=${autoStart} \
-output=$out
'';
'');
};
};
});
Expand Down
2 changes: 0 additions & 2 deletions templates/main.nix.tmpl
Original file line number Diff line number Diff line change
@@ -1,6 +1,4 @@
{{- $runtime := .Runtime | printf "%s" -}}
{ pkgs, ... }:

{
# Runtime
virtualisation.{{$runtime}} = {
Expand Down
2 changes: 0 additions & 2 deletions testdata/TestDocker_WithProject_out.nix
Original file line number Diff line number Diff line change
@@ -1,5 +1,3 @@
{ pkgs, ... }:

{
# Runtime
virtualisation.docker = {
Expand Down
2 changes: 0 additions & 2 deletions testdata/TestDocker_out.nix
Original file line number Diff line number Diff line change
@@ -1,5 +1,3 @@
{ pkgs, ... }:

{
# Runtime
virtualisation.docker = {
Expand Down
2 changes: 0 additions & 2 deletions testdata/TestPodman_WithProject_out.nix
Original file line number Diff line number Diff line change
@@ -1,5 +1,3 @@
{ pkgs, ... }:

{
# Runtime
virtualisation.podman = {
Expand Down
2 changes: 0 additions & 2 deletions testdata/TestPodman_out.nix
Original file line number Diff line number Diff line change
@@ -1,5 +1,3 @@
{ pkgs, ... }:

{
# Runtime
virtualisation.podman = {
Expand Down

0 comments on commit 4e6ac64

Please sign in to comment.