From 8957fe74b1b10b405159e1e6e175911662bdb7e6 Mon Sep 17 00:00:00 2001 From: Julia Lange Date: Thu, 28 Aug 2025 10:50:18 -0700 Subject: [PATCH] Nix, update nixpkgs and add systemd service support --- flake.lock | 24 +++++++------- flake.nix | 95 +++++++++++++++++++++++++++++++++++++++++++++++------- 2 files changed, 96 insertions(+), 23 deletions(-) diff --git a/flake.lock b/flake.lock index 69e701e..c3be24b 100644 --- a/flake.lock +++ b/flake.lock @@ -2,16 +2,18 @@ "nodes": { "nixpkgs": { "locked": { - "lastModified": 1735563628, - "narHash": "sha256-OnSAY7XDSx7CtDoqNh8jwVwh4xNL/2HaJxGjryLWzX8=", - "rev": "b134951a4c9f3c995fd7be05f3243f8ecd65d798", - "revCount": 637546, - "type": "tarball", - "url": "https://api.flakehub.com/f/pinned/NixOS/nixpkgs/0.2405.637546%2Brev-b134951a4c9f3c995fd7be05f3243f8ecd65d798/01941dc2-2ab2-7453-8ebd-88712e28efae/source.tar.gz" + "lastModified": 1752436162, + "narHash": "sha256-Kt1UIPi7kZqkSc5HVj6UY5YLHHEzPBkgpNUByuyxtlw=", + "owner": "NixOS", + "repo": "nixpkgs", + "rev": "dfcd5b901dbab46c9c6e80b265648481aafb01f8", + "type": "github" }, "original": { - "type": "tarball", - "url": "https://flakehub.com/f/NixOS/nixpkgs/0.2405.%2A.tar.gz" + "owner": "NixOS", + "ref": "nixos-25.05", + "repo": "nixpkgs", + "type": "github" } }, "nixpkgs_2": { @@ -41,11 +43,11 @@ "nixpkgs": "nixpkgs_2" }, "locked": { - "lastModified": 1749695868, - "narHash": "sha256-debjTLOyqqsYOUuUGQsAHskFXH5+Kx2t3dOo/FCoNRA=", + "lastModified": 1752547600, + "narHash": "sha256-0vUE42ji4mcCvQO8CI0Oy8LmC6u2G4qpYldZbZ26MLc=", "owner": "oxalica", "repo": "rust-overlay", - "rev": "55f914d5228b5c8120e9e0f9698ed5b7214d09cd", + "rev": "9127ca1f5a785b23a2fc1c74551a27d3e8b9a28b", "type": "github" }, "original": { diff --git a/flake.nix b/flake.nix index 48d94bf..014140f 100644 --- a/flake.nix +++ b/flake.nix @@ -3,13 +3,15 @@ # Flake inputs inputs = { - nixpkgs.url = "https://flakehub.com/f/NixOS/nixpkgs/0.2405.*.tar.gz"; + nixpkgs.url = "github:NixOS/nixpkgs/nixos-25.05"; rust-overlay.url = "github:oxalica/rust-overlay"; # A helper for Rust + Nix }; # Flake outputs outputs = { self, nixpkgs, rust-overlay }: let + pdsDirectory = "/home/pan/prog/atproto/appview"; + # Overlays enable you to customize the Nixpkgs attribute set overlays = [ # Makes a `rust-bin` attribute available in Nixpkgs @@ -33,19 +35,88 @@ forAllSystems = f: nixpkgs.lib.genAttrs allSystems (system: f { pkgs = import nixpkgs { inherit overlays system; }; }); + + # Systemd service configuration + createSystemdService = pkgs: pdsDir: pkgs.writeTextFile { + name = "pds.service"; + text = '' + [Unit] + Description=Development Environment Service + After=network-online.target + Wants=network-online.target + + [Service] + Type=simple + ExecStart=${pkgs.pds}/bin/pds + WorkingDirectory=${pdsDir} + EnvironmentFile=${pdsDir}/.env + Environment=PDS_DATA_DIRECTORY=${pdsDir}/.pds-data + Environment=PDS_BLOBSTORE_DISK_LOCATION=${pdsDir}/.pds-data/blocks + ''; + }; + + # Scripts for managing the systemd service + createServiceScripts = pkgs: pdsDir: + let + serviceFile = createSystemdService pkgs pdsDir; + serviceName = "pds"; + in { + startScript = pkgs.writeShellScript "start-dev-service" '' + set -e + + # Create user systemd directory if it doesn't exist + mkdir -p ~/.config/systemd/user + + # Copy service file + cp -f ${serviceFile} ~/.config/systemd/user/${serviceName}.service + + # Reload systemd and start service + systemctl --user daemon-reload + systemctl --user start ${serviceName} + systemctl --user enable ${serviceName} + + systemctl --user status ${serviceName} --no-pager + ''; + + stopScript = pkgs.writeShellScript "stop-dev-service" '' + set -e + if systemctl --user is-enabled --quiet ${serviceName}; then + # Stop and disable service + systemctl --user stop ${serviceName} || true + systemctl --user disable ${serviceName} || true + + # Remove service file + rm -f ~/.config/systemd/user/${serviceName}.service + + # Reload systemd + systemctl --user daemon-reload + fi + ''; + }; in { # Development environment output - devShells = forAllSystems ({ pkgs }: { - default = pkgs.mkShell { - # The Nix packages provided in the environment - packages = (with pkgs; [ - # The package provided by our custom overlay. Includes cargo, Clippy, cargo-fmt, - # rustdoc, rustfmt, and other tools. - sqlx-cli - rustToolchain - ]) ++ pkgs.lib.optionals pkgs.stdenv.isDarwin (with pkgs; [ libiconv ]); - }; - }); + devShells = forAllSystems ({ pkgs }: + let + scripts = createServiceScripts pkgs pdsDirectory; + in { + default = pkgs.mkShell { + # The Nix packages provided in the environment + packages = (with pkgs; [ + # The package provided by our custom overlay. Includes cargo, Clippy, cargo-fmt, + # rustdoc, rustfmt, and other tools. + sqlx-cli + rustToolchain + ]) ++ pkgs.lib.optionals pkgs.stdenv.isDarwin (with pkgs; [ libiconv ]); + + shellHook = pkgs.lib.optionalString pkgs.stdenv.isLinux '' + # Cleanup + ${scripts.stopScript} + + # Start the systemd service + ${scripts.startScript} + ''; + }; + }); }; }