Skip to content

Instantly share code, notes, and snippets.

@sdorminey
Created April 12, 2019 01:41
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save sdorminey/4335592fce3a7f9c29e2f5de5636afec to your computer and use it in GitHub Desktop.
Save sdorminey/4335592fce3a7f9c29e2f5de5636afec to your computer and use it in GitHub Desktop.
Running into issues with chmod in my container registry module system.
{ config, lib, pkgs, ... }:
with lib;
let
# makeFiles = files:
# let
# files' = filter (f: f.enable) (attrValues files);
# in pkgs.stdenvNoCC.mkDerivation {
# name = "etc";
#
# builder = ./make-files.sh;
#
# preferLocalBuild = true;
# allowSubstitutes = false;
#
# sources = map (x: x.source) files';
# targets = map (x: x.target) files';
# modes = map (x: x.mode) files';
# isDirectory = map (x: if x.isDirectory then "1" else "0" ) files';
# };
#
file-submodule = { name, config, ... }:
{
options = {
enable = mkOption {
type = types.bool;
default = true;
example = "true";
};
source = mkOption {
type = types.nullOr types.path;
default = null;
};
target = mkOption {
type = types.str;
};
text = mkOption {
type = types.lines;
default = "";
};
mode = mkOption {
type = types.str;
example = "744";
default = "744";
};
};
config = {
target = mkDefault "${name}";
source = mkIf (config.text != "") (
let name' = "file-" + baseNameOf name;
in mkDefault (pkgs.writeText name' config.text));
};
};
in {
options = {
name = mkOption {
type = types.str;
example = "redis";
description = "Image name";
};
tag = mkOption {
type = types.str;
default = "latest";
example = "latest";
description = "Image tag.";
};
file = mkOption {
default = {};
example = {};
type = with types; loaOf (submodule [ file-submodule ]);
description = "Extra files to place inside the image, relative to /.";
};
};
}
{ lib
, pkgs
, jq
, dockerTools
}:
let
getConfig = module: (lib.evalModules {
modules = [ module ./container-module.nix ];
specialArgs = { inherit pkgs; };
}).config;
sanitize = file: file // {
target = lib.removePrefix "/" file.target;
};
makeExtraFiles = { name, file }: pkgs.stdenvNoCC.mkDerivation {
name = "extra-files";
builder = ./make-files.sh;
buildInputs = [ jq ];
preferLocalBuild = true;
allowSubstitutes = false;
fileConfig = builtins.toJSON (map sanitize (builtins.attrValues file));
};
extraFiles = path:
let
config = getConfig path;
in makeExtraFiles {
inherit (config) name file;
};
in extraFiles ./example-module.nix
#in {
# buildModularImage = { module }:
# let
# config = getConfig module;
# in dockerTools.buildLayeredImage {
# inherit (config) name tag;
# extraCommands = ''
# '';
# };
#}
{ config, ... }:
{
config = {
name = "foo";
file."/etc/nsswitch.conf".text = ''
hosts: files dns
'';
file."/data".mode = "777";
file."/tmp".mode = "777";
};
}
source $stdenv/setup
mkdir -p $out
set +v
set +x
# Create directories:
echo $out
echo HELLO
echo $fileConfig | jq '.[] | select(.source == null) | "'${out}'/\(.target)"' | xargs -r mkdir -p
echo $fileConfig | jq '.[] | select(.source == null) | "'${out}'/\(.target)"' | xargs -r echo mkdir -p
echo $fileConfig | jq '.[] | select(.source == null) | "\(.mode) '${out}'/\(.target)"' | tr -d \" | xargs -r chmod -R
# Copy files:
mapfile -t cmds < <(echo $fileConfig | jq '.[] | select(.source != null) | "\(.mode) \(.source) \(.target)"' | tr -d \")
for ((k = 0; k < ${#cmds[@]}; k++)); do
args=(${cmds[k]})
mkdir -p $out/$(dirname ${args[2]})
cp ${args[1]} $out/${args[2]}
chmod ${args[0]} $out/${args[2]}
done
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment