我在本地工作站上设置了 NixOS 22.11。
我用额外容器包,它可以像命令式容器一样运行声明性 NixOS 容器,而无需重建系统。
我正在尝试运行一个演示容器。它woocommerce
在容器内下载和压缩,我想在容器和本地系统路径之间同步这些文件。主要目的是为了更好的学习nix。在 docker-compose 中我可以使用这样的配置:
volumes:
- <local-path>:<container-path>
如何在 NixOS 容器中实现它?
我尝试了这些配置,但我的本地系统目录仍然为空:
fileSystems."/var/www/demo/wp-content/plugins/woocommerce" = {
device = "/root/woocommerce";
options = [ "bind" ];
# noCheck = true;
};
};
serviceConfig = {
BindPaths = [ "/var/www/demo/wp-content/plugins/woocommerce:/root/woocommerce" ]
};
完整配置woo.nix
{ pkgs, ... }:
{
containers.demo = {
config = {
fileSystems."/var/www/demo/wp-content/plugins/woocommerce" = {
device = "/root/woocommerce";
options = [ "bind" ];
# noCheck = true;
};
users.users.demo = {
isSystemUser = true;
createHome = true;
group = "demo";
};
users.groups.demo = { };
systemd.services.woo = {
wantedBy = [ "multi-user.target" ];
path = with pkgs; [ coreutils wget gzip curl unzip ];
script = ''
/run/current-system/sw/bin/curl -L https://downloads.wordpress.org/plugin/woocommerce.7.4.1.zip -o /root/woocommerce.zip &&
unzip /root/woocommerce.zip -d /root &&
rm /root/woocommerce.zip
'';
serviceConfig = {
BindPaths = [ "/var/www/demo/wp-content/plugins/woocommerce:/root/woocommerce" ];
# ReadWriteDirectories = [ "/root/woocommerce" ];
};
};
};
};
environment.systemPackages = [ pkgs.coreutils pkgs.wget pkgs.gzip pkgs.curl pkgs.unzip ];
}
我用以下命令运行它:
sudo extra-container create --start <<EOF
$(cat woo.nix)
EOF
在 NixOS 声明性容器和本地系统之间同步文件的最佳方法是什么?
答案1
这个bindMounts
选项正是我所需要的。
containers.demo = {
bindMounts = {
"/root/woocommerce" = {
hostPath = "/var/www/demo/wp-content/plugins/woocommerce";
isReadOnly = false;
};
};
}