在AliOS或者龙蜥OS上采用systemd-nspwan部署开发环境和NixOS

简介: # 1. 首先你需要个docker-ce```bashyum install docker-ce -b test```找个地方存在你的NixOS的根目录```DEST=/opt/weiping.xwp/nixosmkdir -p $DEST/{dev,proc,etc/nixos}docker run --privileged -v $DEST:/mnt -it --rm --

1. 首先你需要个docker-ce

yum install docker-ce -b test

找个地方存在你的NixOS的根目录

DEST=/opt/weiping.xwp/nixos
mkdir -p $DEST/{dev,proc,etc/nixos}

docker run --privileged -v $DEST:/mnt -it --rm --cap-add SYS_ADMIN docker.io/nixos/nix:latest

2. 你需要设置国内镜像

容器里运行

echo "substituters = https://mirrors.tuna.tsinghua.edu.cn/nix-channels/store https://mirrors.tuna.tsinghua.edu.cn/nix-channels/store https://cache.nixos.org/" >> /etc/nix/nix.conf
nix-channel --add https://mirrors.tuna.tsinghua.edu.cn/nix-channels/nixos-22.11 nixpkgs
nix-channel --update
nix-channel --list

Install the nixos install tools:

nix-env -f '<nixpkgs>' -iA nixos-install-tools util-linux

Copy the configuration.nix file and run the installation:

cat > /mnt/etc/nixos/configuration.nix
…see below…
<Ctrl-D>

# workaround for error: while setting up the build environment: mounting /proc: Operation not permitted «
mount --bind /proc/ /mnt/proc
mount --bind /dev/ /mnt/dev

nixos-install --root /mnt
# /etc/nixos/configuration.nix
#
# Edit this configuration file to define what should be installed on
# your system.  Help is available in the configuration.nix(5) man page
# and in the NixOS manual (accessible by running ‘nixos-help’).

{ config, pkgs, ... }:

{
  imports = [];

  boot.isContainer = true;
  boot.loader.initScript.enable = true;

  time.timeZone = "Europe/Skopje";

  networking.hostName = ""; # empty
  networking.useDHCP = false;
  networking.useNetworkd = true;
  networking.useHostResolvConf = false;
  networking.firewall.enable = false;
  # default password is "root", create with `openssl passwd -6 root`
  users.users.root.initialHashedPassword = "$6$V1JB3DXzfkBBjaxL$V4ymu8BxUdDKwDqRMsy4bu4tyocBglz6qtuyonMbi.HweoKbcgLr.W57A62SPqi6CzEGWtER9vskXHAqoHpr4/";
    nix.settings.substituters = [ "https://mirrors.tuna.tsinghua.edu.cn/nix-channels/store" "https://mirrors4.bfsu.edu.cn/nix-channels/store" ];

  environment.systemPackages = with pkgs; [
     vim
     wget
  ];
  # services.sshd.enable = true;

  system.stateVersion = "22.11";
}

3. 准备网络

brctl addbr  br0
ip addr add 192.168.122.1/24 dev br0

yum install dnsmasq
cat > /etc/dnsmasq.conf
strict-order
except-interface=lo
bind-dynamic
interface=virbr0
dhcp-range=192.168.122.2,192.168.122.254
dhcp-no-override
dhcp-authoritative
dhcp-lease-max=253
<Ctrl-D>

systemctl start dnsmasq.service && systemctl enable dnsmasq.service

iptables -t nat -A POSTROUTING -s 192.168.122.0/24 -o eth0 -j MASQUERADE

4. Boot

sudo systemd-nspawn --network-bridge=virbr0 --directory $DEST -- /sbin/init
…
-p 10000:20000 that host port 10000 is mapped to container port 20000

# poweroff

5. 转入后台运行

创建一个systemd unit

cat  > /usr/lib/systemd/system/systemd-nspawn@nixos.service
[Unit]
Description=Container
Documentation=man:systemd-nspawn(1)
After=network.target

[Service]
ExecStart=/usr/bin/systemd-nspawn --quiet --keep-unit --link-journal=try-guest --network-bridge=virbr0 --directory=/opt/weiping.xwp/nixos -- /sbin/init
KillMode=mixed
Type=notify
RestartForceExitStatus=133
SuccessExitStatus=133
Slice=machine.slice
Delegate=yes

[Install]
WantedBy=machines.target
systemctl start systemd-nspawn@nixos.service

进入NixOS的方式
machine start nixos

如果你用systemd-nspwan的自注册能力,只需要将根目录转移到 /var/lib/machines/nixos

这样你就拥有一个NixOS初始环境,并且可以联通外网,如果需要对外提供服务,做下DNAT放通相应端口,或者使用systemd-nspawn的-p参数即可

目录
相关文章
|
9天前
|
人工智能 Anolis
2025 北京文化论坛阿里巴巴沙龙落幕 分享龙蜥在操作系统领域的创新实践
分享龙蜥社区以及其理事长单位阿里云在AI 开源领域的战略布局。
|
9天前
|
人工智能 安全 云计算
|
2月前
|
安全 编译器 Linux
|
5月前
|
Java 关系型数据库 MySQL
在Linux操作系统上设置JDK、Tomcat、MySQL以及J2EE后端接口的部署步骤
让我们总结一下,给你的Linux操作系统装备上最强的军队,需要先后装备好JDK的弓箭,布置好Tomcat的阵地,再把MySQL的物资原料准备好,最后部署好J2EE攻城车,那就准备好进军吧,你的Linux军团,无人可挡!
141 18
|
5月前
|
人工智能 安全 算法
|
5月前
|
开发框架 关系型数据库 Java
Linux操作系统中JDK、Tomcat、MySQL的完整安装流程以及J2EE后端接口的部署
然后Tomcat会自动将其解压成一个名为ROOT的文件夹。重启Tomcat,让新“植物”适应新环境。访问http://localhost:8080/yourproject看到你的项目页面,说明“植物”种植成功。
154 10
|
4月前
|
Cloud Native 安全 Linux
龙蜥操作系统:CentOS 谢幕之后,国产云原生系统的崛起之路
龙蜥操作系统(Anolis OS)是 CentOS 停止维护后,由阿里云等企业联合发起的开源项目。它以双内核架构和全栈优化为核心,提供无缝替代 CentOS 的方案,兼容主流生态并针对云计算场景深度优化。其技术亮点包括 RHCK 和 ANCK 双内核、性能优化、全栈安全及国密算法支持。龙蜥适用于云原生基础设施、企业级应用部署及开发环境,社区已吸引 200 多家单位参与。未来规划涵盖 AI 框架优化、RISC-V 架构适配及桌面环境构建,正重新定义云时代的操作系统边界。
887 0

推荐镜像

更多