diff --git a/.gitignore b/.gitignore index dd0efb3..b5cf175 100644 --- a/.gitignore +++ b/.gitignore @@ -7,6 +7,10 @@ /guest/out/ .wrangler/ +# Nix build symlinks +result +result-* + # Built binaries and downloaded installers: never in the repo. The Windows # bundle (cargo xtask windows) lands under /target and is shared from there. *.exe diff --git a/flake.lock b/flake.lock new file mode 100644 index 0000000..6c335e2 --- /dev/null +++ b/flake.lock @@ -0,0 +1,27 @@ +{ + "nodes": { + "nixpkgs": { + "locked": { + "lastModified": 1794537146, + "narHash": "sha256-Wpz/7x/YWnDIBu7yj6ixe8j6fIMsidd9Y+o1PqPF3iM=", + "owner": "NixOS", + "repo": "nixpkgs", + "rev": "d5cc6833999746cffdfe0598f7bc801a57b887a9", + "type": "github" + }, + "original": { + "owner": "NixOS", + "ref": "nixos-unstable", + "repo": "nixpkgs", + "type": "github" + } + }, + "root": { + "inputs": { + "nixpkgs": "nixpkgs" + } + } + }, + "root": "root", + "version": 7 +} diff --git a/flake.nix b/flake.nix new file mode 100644 index 0000000..61476d1 --- /dev/null +++ b/flake.nix @@ -0,0 +1,105 @@ +{ + description = "Kiri container engine"; + + inputs.nixpkgs.url = "github:NixOS/nixpkgs/nixos-unstable"; + + outputs = { self, nixpkgs }: + let + systems = [ "x86_64-linux" "aarch64-linux" ]; + forAllSystems = nixpkgs.lib.genAttrs systems; + packages = forAllSystems (system: + let + pkgs = import nixpkgs { inherit system; }; + manifestVersion = (builtins.fromTOML (builtins.readFile ./Cargo.toml)).workspace.package.version; + kiri = pkgs.rustPlatform.buildRustPackage { + pname = "kiri"; + version = + if manifestVersion != "0.0.0" then manifestVersion + else "0.0.0-${self.shortRev or self.dirtyShortRev or "dev"}"; + src = self; + + cargoLock.lockFile = ./Cargo.lock; + cargoBuildFlags = [ "-p" "kiri" "-p" "kiri-init" ]; + cargoTestFlags = [ "-p" "kiri" "-p" "kiri-init" ]; + + # Some unit tests inspect /home or require real root namespaces absent in the Nix build sandbox. + doCheck = false; + + postInstall = '' + install -Dm644 packaging/linux/dev.kiri.host-access.policy \ + "$out/share/polkit-1/actions/dev.kiri.host-access.policy" + ''; + + meta = { + description = "Small, fast container runtime"; + homepage = "https://runkiri.com/"; + license = pkgs.lib.licenses.asl20; + platforms = systems; + mainProgram = "kiri"; + }; + }; + in + { + inherit kiri; + default = kiri; + }); + in + { + packages = packages; + + nixosModules.default = { config, lib, pkgs, ... }: + let + cfg = config.services.kiri; + in + { + options.services.kiri = { + enable = lib.mkEnableOption "the Kiri container engine"; + package = lib.mkOption { + type = lib.types.package; + default = self.packages.${pkgs.stdenv.hostPlatform.system}.kiri; + defaultText = lib.literalExpression "inputs.kiri.packages.\${pkgs.stdenv.hostPlatform.system}.kiri"; + description = "The Kiri package to run and install."; + }; + }; + + config = lib.mkIf cfg.enable { + environment.systemPackages = [ cfg.package ]; + users.groups.kiri = { }; + + environment.etc."NetworkManager/conf.d/kiri.conf" = lib.mkIf config.networking.networkmanager.enable { + source = ./packaging/linux/kiri-nm.conf; + }; + + systemd.services.kiri = { + description = "Kiri container engine"; + documentation = [ "https://runkiri.com" ]; + wantedBy = [ "multi-user.target" ]; + wants = [ "network-online.target" ]; + after = [ "network-online.target" "docker.service" ]; + path = [ pkgs.nftables pkgs.iptables pkgs.coreutils ]; + serviceConfig = { + ExecStart = "${cfg.package}/bin/kiri daemon"; + ExecStartPost = "-${pkgs.runtimeShell} -c '[ \"$(readlink /run/docker.sock)\" = /run/kiri/docker.sock ] || [ -e /run/docker.sock ] || [ -L /run/docker.sock ] || ln -s /run/kiri/docker.sock /run/docker.sock'"; + ExecStopPost = "-${pkgs.runtimeShell} -c '[ \"$(readlink /run/docker.sock)\" != /run/kiri/docker.sock ] || rm -f /run/docker.sock'"; + Environment = [ + "KIRI_SOCKET=/run/kiri/docker.sock" + "KIRI_SOCKET_GROUP=kiri" + "HOME=/var/lib/kiri-daemon" + ]; + Delegate = true; + LimitNPROC = "infinity"; + LimitCORE = "infinity"; + TasksMax = "infinity"; + KillMode = "mixed"; + Restart = "on-failure"; + RuntimeDirectory = "kiri"; + RuntimeDirectoryMode = "0755"; + StateDirectory = "kiri-daemon"; + StateDirectoryMode = "0700"; + UMask = "0027"; + }; + }; + }; + }; + }; +} diff --git a/packaging/README.md b/packaging/README.md index 212096d..856dbde 100644 --- a/packaging/README.md +++ b/packaging/README.md @@ -95,6 +95,15 @@ application-menu launcher and icons, without replacing an installed Kiri engine, CLI, systemd service, Docker context, or container store. See `arch/README.md` for build and install steps. +## Nix and NixOS + +`flake.nix` builds `kiri` and `kiri-init` on `x86_64-linux` and `aarch64-linux` +(`nix build .#kiri`), and exports `nixosModules.default` (`services.kiri.enable = true;`), +which creates the `kiri` group, configures NetworkManager to leave Kiri's +bridges and veths unmanaged, and runs `kiri.service` with the same cgroup +delegation, resource limits, and `/run/docker.sock` fallback link as the `.deb` +and `.rpm` packages. + ## Homebrew (planned, not built) The Mac already has the app and the dmg; Homebrew is the third way in, for