2021-10-30 22:22:35 +00:00
|
|
|
{ pkgs ? import <nixpkgs> { }
|
|
|
|
, lib ? pkgs.lib
|
|
|
|
, name ? "nix"
|
|
|
|
, tag ? "latest"
|
|
|
|
, channelName ? "nixpkgs"
|
|
|
|
, channelURL ? "https://nixos.org/channels/nixpkgs-unstable"
|
|
|
|
}:
|
|
|
|
let
|
2021-11-16 15:32:26 +00:00
|
|
|
defaultPkgs = with pkgs; [
|
2021-11-02 13:03:55 +00:00
|
|
|
nix
|
|
|
|
bashInteractive
|
|
|
|
coreutils-full
|
|
|
|
gnutar
|
|
|
|
gzip
|
|
|
|
gnugrep
|
|
|
|
which
|
|
|
|
curl
|
|
|
|
less
|
|
|
|
wget
|
|
|
|
man
|
|
|
|
cacert.out
|
|
|
|
findutils
|
2022-01-04 11:49:02 +00:00
|
|
|
iana-etc
|
2022-01-14 14:55:36 +00:00
|
|
|
git
|
2022-04-14 12:49:47 +00:00
|
|
|
openssh
|
2021-10-30 22:22:35 +00:00
|
|
|
];
|
|
|
|
|
|
|
|
users = {
|
|
|
|
|
|
|
|
root = {
|
|
|
|
uid = 0;
|
|
|
|
shell = "/bin/bash";
|
|
|
|
home = "/root";
|
|
|
|
gid = 0;
|
|
|
|
};
|
|
|
|
|
|
|
|
} // lib.listToAttrs (
|
|
|
|
map
|
|
|
|
(
|
|
|
|
n: {
|
|
|
|
name = "nixbld${toString n}";
|
|
|
|
value = {
|
|
|
|
uid = 30000 + n;
|
|
|
|
gid = 30000;
|
|
|
|
groups = [ "nixbld" ];
|
|
|
|
description = "Nix build user ${toString n}";
|
|
|
|
};
|
|
|
|
}
|
|
|
|
)
|
|
|
|
(lib.lists.range 1 32)
|
|
|
|
);
|
|
|
|
|
|
|
|
groups = {
|
|
|
|
root.gid = 0;
|
|
|
|
nixbld.gid = 30000;
|
|
|
|
};
|
|
|
|
|
|
|
|
userToPasswd = (
|
|
|
|
k:
|
|
|
|
{ uid
|
|
|
|
, gid ? 65534
|
|
|
|
, home ? "/var/empty"
|
|
|
|
, description ? ""
|
|
|
|
, shell ? "/bin/false"
|
|
|
|
, groups ? [ ]
|
|
|
|
}: "${k}:x:${toString uid}:${toString gid}:${description}:${home}:${shell}"
|
|
|
|
);
|
|
|
|
passwdContents = (
|
|
|
|
lib.concatStringsSep "\n"
|
|
|
|
(lib.attrValues (lib.mapAttrs userToPasswd users))
|
|
|
|
);
|
|
|
|
|
|
|
|
userToShadow = k: { ... }: "${k}:!:1::::::";
|
|
|
|
shadowContents = (
|
|
|
|
lib.concatStringsSep "\n"
|
|
|
|
(lib.attrValues (lib.mapAttrs userToShadow users))
|
|
|
|
);
|
|
|
|
|
|
|
|
# Map groups to members
|
|
|
|
# {
|
|
|
|
# group = [ "user1" "user2" ];
|
|
|
|
# }
|
|
|
|
groupMemberMap = (
|
|
|
|
let
|
|
|
|
# Create a flat list of user/group mappings
|
|
|
|
mappings = (
|
|
|
|
builtins.foldl'
|
|
|
|
(
|
|
|
|
acc: user:
|
|
|
|
let
|
|
|
|
groups = users.${user}.groups or [ ];
|
|
|
|
in
|
|
|
|
acc ++ map
|
|
|
|
(group: {
|
|
|
|
inherit user group;
|
|
|
|
})
|
|
|
|
groups
|
|
|
|
)
|
|
|
|
[ ]
|
|
|
|
(lib.attrNames users)
|
|
|
|
);
|
|
|
|
in
|
|
|
|
(
|
|
|
|
builtins.foldl'
|
|
|
|
(
|
|
|
|
acc: v: acc // {
|
|
|
|
${v.group} = acc.${v.group} or [ ] ++ [ v.user ];
|
|
|
|
}
|
|
|
|
)
|
|
|
|
{ }
|
|
|
|
mappings)
|
|
|
|
);
|
|
|
|
|
|
|
|
groupToGroup = k: { gid }:
|
|
|
|
let
|
|
|
|
members = groupMemberMap.${k} or [ ];
|
|
|
|
in
|
|
|
|
"${k}:x:${toString gid}:${lib.concatStringsSep "," members}";
|
|
|
|
groupContents = (
|
|
|
|
lib.concatStringsSep "\n"
|
|
|
|
(lib.attrValues (lib.mapAttrs groupToGroup groups))
|
|
|
|
);
|
|
|
|
|
|
|
|
nixConf = {
|
|
|
|
sandbox = "false";
|
|
|
|
build-users-group = "nixbld";
|
|
|
|
trusted-public-keys = "cache.nixos.org-1:6NCHdD59X431o0gWypbMrAURkbJ16ZPMQFGspcDShjY=";
|
2022-06-08 15:41:31 +00:00
|
|
|
ignored-acls = "security.csm";
|
2021-10-30 22:22:35 +00:00
|
|
|
};
|
|
|
|
nixConfContents = (lib.concatStringsSep "\n" (lib.mapAttrsFlatten (n: v: "${n} = ${v}") nixConf)) + "\n";
|
|
|
|
|
|
|
|
baseSystem =
|
|
|
|
let
|
2021-11-16 15:32:26 +00:00
|
|
|
nixpkgs = pkgs.path;
|
|
|
|
channel = pkgs.runCommand "channel-nixos" { } ''
|
2021-10-30 22:22:35 +00:00
|
|
|
mkdir $out
|
|
|
|
ln -s ${nixpkgs} $out/nixpkgs
|
|
|
|
echo "[]" > $out/manifest.nix
|
|
|
|
'';
|
2021-11-16 15:32:26 +00:00
|
|
|
rootEnv = pkgs.buildPackages.buildEnv {
|
2021-10-30 22:22:35 +00:00
|
|
|
name = "root-profile-env";
|
|
|
|
paths = defaultPkgs;
|
|
|
|
};
|
2021-12-16 10:29:05 +00:00
|
|
|
manifest = pkgs.buildPackages.runCommand "manifest.nix" { } ''
|
|
|
|
cat > $out <<EOF
|
2021-10-30 22:22:35 +00:00
|
|
|
[
|
|
|
|
${lib.concatStringsSep "\n" (builtins.map (drv: let
|
|
|
|
outputs = drv.outputsToInstall or [ "out" ];
|
|
|
|
in ''
|
|
|
|
{
|
|
|
|
${lib.concatStringsSep "\n" (builtins.map (output: ''
|
|
|
|
${output} = { outPath = "${lib.getOutput output drv}"; };
|
|
|
|
'') outputs)}
|
|
|
|
outputs = [ ${lib.concatStringsSep " " (builtins.map (x: "\"${x}\"") outputs)} ];
|
|
|
|
name = "${drv.name}";
|
|
|
|
outPath = "${drv}";
|
|
|
|
system = "${drv.system}";
|
|
|
|
type = "derivation";
|
|
|
|
meta = { };
|
|
|
|
}
|
|
|
|
'') defaultPkgs)}
|
|
|
|
]
|
|
|
|
EOF
|
|
|
|
'';
|
2021-12-16 10:29:05 +00:00
|
|
|
profile = pkgs.buildPackages.runCommand "user-environment" { } ''
|
|
|
|
mkdir $out
|
|
|
|
cp -a ${rootEnv}/* $out/
|
|
|
|
ln -s ${manifest} $out/manifest.nix
|
|
|
|
'';
|
2021-10-30 22:22:35 +00:00
|
|
|
in
|
2021-11-16 15:32:26 +00:00
|
|
|
pkgs.runCommand "base-system"
|
2021-10-30 22:22:35 +00:00
|
|
|
{
|
|
|
|
inherit passwdContents groupContents shadowContents nixConfContents;
|
|
|
|
passAsFile = [
|
|
|
|
"passwdContents"
|
|
|
|
"groupContents"
|
|
|
|
"shadowContents"
|
|
|
|
"nixConfContents"
|
|
|
|
];
|
|
|
|
allowSubstitutes = false;
|
|
|
|
preferLocalBuild = true;
|
|
|
|
} ''
|
|
|
|
env
|
|
|
|
set -x
|
|
|
|
mkdir -p $out/etc
|
|
|
|
|
2021-12-21 20:17:29 +00:00
|
|
|
mkdir -p $out/etc/ssl/certs
|
|
|
|
ln -s /nix/var/nix/profiles/default/etc/ssl/certs/ca-bundle.crt $out/etc/ssl/certs
|
|
|
|
|
2021-10-30 22:22:35 +00:00
|
|
|
cat $passwdContentsPath > $out/etc/passwd
|
|
|
|
echo "" >> $out/etc/passwd
|
|
|
|
|
|
|
|
cat $groupContentsPath > $out/etc/group
|
|
|
|
echo "" >> $out/etc/group
|
|
|
|
|
|
|
|
cat $shadowContentsPath > $out/etc/shadow
|
|
|
|
echo "" >> $out/etc/shadow
|
|
|
|
|
|
|
|
mkdir -p $out/usr
|
|
|
|
ln -s /nix/var/nix/profiles/share $out/usr/
|
|
|
|
|
|
|
|
mkdir -p $out/nix/var/nix/gcroots
|
|
|
|
|
|
|
|
mkdir $out/tmp
|
|
|
|
|
2022-01-15 13:11:37 +00:00
|
|
|
mkdir -p $out/var/tmp
|
2022-01-15 09:20:18 +00:00
|
|
|
|
2021-10-30 22:22:35 +00:00
|
|
|
mkdir -p $out/etc/nix
|
|
|
|
cat $nixConfContentsPath > $out/etc/nix/nix.conf
|
|
|
|
|
|
|
|
mkdir -p $out/root
|
|
|
|
mkdir -p $out/nix/var/nix/profiles/per-user/root
|
|
|
|
|
|
|
|
ln -s ${profile} $out/nix/var/nix/profiles/default-1-link
|
|
|
|
ln -s $out/nix/var/nix/profiles/default-1-link $out/nix/var/nix/profiles/default
|
|
|
|
ln -s /nix/var/nix/profiles/default $out/root/.nix-profile
|
|
|
|
|
|
|
|
ln -s ${channel} $out/nix/var/nix/profiles/per-user/root/channels-1-link
|
|
|
|
ln -s $out/nix/var/nix/profiles/per-user/root/channels-1-link $out/nix/var/nix/profiles/per-user/root/channels
|
|
|
|
|
|
|
|
mkdir -p $out/root/.nix-defexpr
|
|
|
|
ln -s $out/nix/var/nix/profiles/per-user/root/channels $out/root/.nix-defexpr/channels
|
|
|
|
echo "${channelURL} ${channelName}" > $out/root/.nix-channels
|
|
|
|
|
|
|
|
mkdir -p $out/bin $out/usr/bin
|
2021-11-16 15:32:26 +00:00
|
|
|
ln -s ${pkgs.coreutils}/bin/env $out/usr/bin/env
|
|
|
|
ln -s ${pkgs.bashInteractive}/bin/bash $out/bin/sh
|
2021-10-30 22:22:35 +00:00
|
|
|
'';
|
|
|
|
|
|
|
|
in
|
2021-11-16 15:32:26 +00:00
|
|
|
pkgs.dockerTools.buildLayeredImageWithNixDb {
|
2021-10-30 22:22:35 +00:00
|
|
|
|
|
|
|
inherit name tag;
|
|
|
|
|
|
|
|
contents = [ baseSystem ];
|
|
|
|
|
|
|
|
extraCommands = ''
|
|
|
|
rm -rf nix-support
|
|
|
|
ln -s /nix/var/nix/profiles nix/var/nix/gcroots/profiles
|
|
|
|
'';
|
2022-01-01 15:47:05 +00:00
|
|
|
fakeRootCommands = ''
|
|
|
|
chmod 1777 tmp
|
2022-01-15 09:20:18 +00:00
|
|
|
chmod 1777 var/tmp
|
2022-01-01 15:47:05 +00:00
|
|
|
'';
|
2021-10-30 22:22:35 +00:00
|
|
|
|
|
|
|
config = {
|
|
|
|
Cmd = [ "/root/.nix-profile/bin/bash" ];
|
|
|
|
Env = [
|
|
|
|
"USER=root"
|
|
|
|
"PATH=${lib.concatStringsSep ":" [
|
|
|
|
"/root/.nix-profile/bin"
|
|
|
|
"/nix/var/nix/profiles/default/bin"
|
|
|
|
"/nix/var/nix/profiles/default/sbin"
|
|
|
|
]}"
|
|
|
|
"MANPATH=${lib.concatStringsSep ":" [
|
|
|
|
"/root/.nix-profile/share/man"
|
|
|
|
"/nix/var/nix/profiles/default/share/man"
|
|
|
|
]}"
|
|
|
|
"SSL_CERT_FILE=/nix/var/nix/profiles/default/etc/ssl/certs/ca-bundle.crt"
|
|
|
|
"GIT_SSL_CAINFO=/nix/var/nix/profiles/default/etc/ssl/certs/ca-bundle.crt"
|
|
|
|
"NIX_SSL_CERT_FILE=/nix/var/nix/profiles/default/etc/ssl/certs/ca-bundle.crt"
|
|
|
|
"NIX_PATH=/nix/var/nix/profiles/per-user/root/channels:/root/.nix-defexpr/channels"
|
|
|
|
];
|
|
|
|
};
|
|
|
|
|
|
|
|
}
|