Skip to content

Commit

Permalink
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
chore: reformat modules/containers.nix
Browse files Browse the repository at this point in the history
blackheaven committed Aug 11, 2024
1 parent 25e48e9 commit 26fd708
Showing 1 changed file with 127 additions and 117 deletions.
244 changes: 127 additions & 117 deletions src/modules/containers.nix
Original file line number Diff line number Diff line change
@@ -2,9 +2,11 @@

let
projectName = name:
if config.name == null
then throw ''You need to set `name = "myproject";` or `containers.${name}.name = "mycontainer"; to be able to generate a container.''
else config.name;
if config.name == null then
throw ''
You need to set `name = "myproject";` or `containers.${name}.name = "mycontainer"; to be able to generate a container.''
else
config.name;
types = lib.types;
envContainerName = builtins.getEnv "DEVENV_CONTAINER";

@@ -20,38 +22,43 @@ let
url = "github:rrbutani/nix-mk-shell-bin";
attribute = "containers";
};
shell = mk-shell-bin.lib.mkShellBin { drv = config.shell; nixpkgs = pkgs; };
shell = mk-shell-bin.lib.mkShellBin {
drv = config.shell;
nixpkgs = pkgs;
};
bash = "${pkgs.bashInteractive}/bin/bash";
mkEntrypoint = cfg: pkgs.writeScript "entrypoint" ''
#!${bash}
mkEntrypoint = cfg:
pkgs.writeScript "entrypoint" ''
#!${bash}
export PATH=/bin
export PATH=/bin
source ${shell.envScript}
source ${shell.envScript}
# expand any envvars before exec
cmd="`echo "$@"|${pkgs.envsubst}/bin/envsubst`"
# expand any envvars before exec
cmd="`echo "$@"|${pkgs.envsubst}/bin/envsubst`"
${bash} -c "$cmd"
'';
${bash} -c "$cmd"
'';
user = "user";
group = "user";
uid = "1000";
gid = "1000";
homeDir = "/env";

mkHome = path: (pkgs.runCommand "devenv-container-home" { } ''
mkdir -p $out${homeDir}
cp -R ${path}/* $out${homeDir}/
'');
mkHome = path:
(pkgs.runCommand "devenv-container-home" { } ''
mkdir -p $out${homeDir}
cp -R ${path}/* $out${homeDir}/
'');

mkMultiHome = paths: map mkHome paths;

homeRoots = cfg: (
if (builtins.typeOf cfg.copyToRoot == "list")
then cfg.copyToRoot
else [ cfg.copyToRoot ]
);
homeRoots = cfg:
(if (builtins.typeOf cfg.copyToRoot == "list") then
cfg.copyToRoot
else
[ cfg.copyToRoot ]);

mkTmp = (pkgs.runCommand "devenv-container-tmp" { } ''
mkdir -p $out/tmp
@@ -81,101 +88,97 @@ let
touch $out/etc/login.defs
'');

mkPerm = derivation:
{
path = derivation;
mode = "0744";
uid = lib.toInt uid;
gid = lib.toInt gid;
uname = user;
gname = group;
};

mkPerm = derivation: {
path = derivation;
mode = "0744";
uid = lib.toInt uid;
gid = lib.toInt gid;
uname = user;
gname = group;
};

mkDerivation = cfg: nix2container.nix2container.buildImage {
name = cfg.name;
tag = cfg.version;
initializeNixDatabase = true;
nixUid = lib.toInt uid;
nixGid = lib.toInt gid;

copyToRoot = [
(pkgs.buildEnv {
name = "devenv-container-root";
paths = [
pkgs.coreutils-full
pkgs.bashInteractive
pkgs.su
pkgs.sudo
];
pathsToLink = "/bin";
})
mkEtc
mkTmp
];

maxLayers = cfg.maxLayers;

layers = [
(nix2container.nix2container.buildLayer {
perms = map mkPerm (mkMultiHome (homeRoots cfg));
copyToRoot = mkMultiHome (homeRoots cfg);
})
];

perms = [
{
mkDerivation = cfg:
nix2container.nix2container.buildImage {
name = cfg.name;
tag = cfg.version;
initializeNixDatabase = true;
nixUid = lib.toInt uid;
nixGid = lib.toInt gid;

copyToRoot = [
(pkgs.buildEnv {
name = "devenv-container-root";
paths =
[ pkgs.coreutils-full pkgs.bashInteractive pkgs.su pkgs.sudo ];
pathsToLink = "/bin";
})
mkEtc
mkTmp
];

maxLayers = cfg.maxLayers;

layers = [
(nix2container.nix2container.buildLayer {
perms = map mkPerm (mkMultiHome (homeRoots cfg));
copyToRoot = mkMultiHome (homeRoots cfg);
})
];

perms = [{
path = mkTmp;
regex = "/tmp";
mode = "1777";
uid = 0;
gid = 0;
uname = "root";
gname = "root";
}
];

config = {
Entrypoint = cfg.entrypoint;
User = "${user}";
WorkingDir = "${homeDir}";
Env = lib.mapAttrsToList
(name: value:
"${name}=${toString value}"
)
config.env ++ [ "HOME=${homeDir}" "USER=${user}" ];
Cmd = [ cfg.startupCommand ];
}];

config = {
Entrypoint = cfg.entrypoint;
User = "${user}";
WorkingDir = "${homeDir}";
Env = lib.mapAttrsToList (name: value: "${name}=${toString value}")
config.env ++ [ "HOME=${homeDir}" "USER=${user}" ];
Cmd = [ cfg.startupCommand ];
};
};
};

# <registry> <args>
mkCopyScript = cfg: pkgs.writeShellScript "copy-container" ''
set -e -o pipefail
container=$1
shift
if [[ "$1" == false ]]; then
registry=${cfg.registry}
else
registry="$1"
fi
shift
dest="''${registry}${cfg.name}:${cfg.version}"
if [[ $# == 0 ]]; then
args=(${if cfg.defaultCopyArgs == [] then "" else toString cfg.defaultCopyArgs})
else
args=("$@")
fi
echo
echo "Copying container $container to $dest"
echo
${nix2container.skopeo-nix2container}/bin/skopeo --insecure-policy copy "nix:$container" "$dest" ''${args[@]}
'';
mkCopyScript = cfg:
pkgs.writeShellScript "copy-container" ''
set -e -o pipefail
container=$1
shift
if [[ "$1" == false ]]; then
registry=${cfg.registry}
else
registry="$1"
fi
shift
dest="''${registry}${cfg.name}:${cfg.version}"
if [[ $# == 0 ]]; then
args=(${
if cfg.defaultCopyArgs == [ ] then
""
else
toString cfg.defaultCopyArgs
})
else
args=("$@")
fi
echo
echo "Copying container $container to $dest"
echo
${nix2container.skopeo-nix2container}/bin/skopeo --insecure-policy copy "nix:$container" "$dest" ''${args[@]}
'';
containerOptions = types.submodule ({ name, config, ... }: {
options = {
name = lib.mkOption {
@@ -193,7 +196,8 @@ let

copyToRoot = lib.mkOption {
type = types.either types.path (types.listOf types.path);
description = "Add a path to the container. Defaults to the whole git repo.";
description =
"Add a path to the container. Defaults to the whole git repo.";
default = self;
defaultText = "self";
};
@@ -213,11 +217,10 @@ let

defaultCopyArgs = lib.mkOption {
type = types.listOf types.str;
description =
''
Default arguments to pass to `skopeo copy`.
You can override them by passing arguments to the script.
'';
description = ''
Default arguments to pass to `skopeo copy`.
You can override them by passing arguments to the script.
'';
default = [ ];
};

@@ -236,7 +239,8 @@ let
isBuilding = lib.mkOption {
type = types.bool;
default = false;
description = "Set to true when the environment is building this container.";
description =
"Set to true when the environment is building this container.";
};

derivation = lib.mkOption {
@@ -266,14 +270,16 @@ in
containers = lib.mkOption {
type = types.attrsOf containerOptions;
default = { };
description = "Container specifications that can be built, copied and ran using `devenv container`.";
description =
"Container specifications that can be built, copied and ran using `devenv container`.";
};

container = {
isBuilding = lib.mkOption {
type = types.bool;
default = false;
description = "Set to true when the environment is building a container.";
description =
"Set to true when the environment is building a container.";
};
};
};
@@ -292,12 +298,16 @@ in
startupCommand = lib.mkDefault config.procfileScript;
};
}
(if envContainerName == "" then { } else {
(if envContainerName == "" then
{ }
else {
containers.${envContainerName}.isBuilding = true;
})
(lib.mkIf config.container.isBuilding {
devenv.tmpdir = lib.mkOverride (lib.modules.defaultOverridePriority - 1) "/tmp";
devenv.runtime = lib.mkOverride (lib.modules.defaultOverridePriority - 1) "${config.devenv.tmpdir}/devenv";
devenv.tmpdir =
lib.mkOverride (lib.modules.defaultOverridePriority - 1) "/tmp";
devenv.runtime = lib.mkOverride (lib.modules.defaultOverridePriority - 1)
"${config.devenv.tmpdir}/devenv";
devenv.root = lib.mkForce "${homeDir}";
devenv.dotfile = lib.mkOverride 49 "${homeDir}/.devenv";
})

0 comments on commit 26fd708

Please sign in to comment.