Skip to content

feat(k3s): add k3s systemd service and kubeconfig for kyber - #1580

Merged
shunkakinoki merged 5 commits into
mainfrom
feat/k3s-service
Apr 26, 2026
Merged

feat(k3s): add k3s systemd service and kubeconfig for kyber#1580
shunkakinoki merged 5 commits into
mainfrom
feat/k3s-service

Conversation

@shunkakinoki

@shunkakinoki shunkakinoki commented Apr 26, 2026

Copy link
Copy Markdown
Owner

Summary

  • Add services/k3s/ module: installs pkgs.k3s and manages a systemd system service on kyber via activation script
  • Add config/kube/ module: copies k3s kubeconfig to ~/.kube/config for non-root kubectl access (kyber-only)
  • Register both modules in their respective default.nix indexes

Test plan

  • Run home-manager switch --flake .#kyber on kyber and verify k3s service starts
  • Verify kubectl get nodes works without sudo after activation
  • Verify non-kyber hosts are unaffected (modules gated by host.isKyber)

🤖 Generated with Claude Code


Summary by cubic

Add a k3s systemd service and kubeconfig setup on kyber to run Kubernetes without manual installs. Also ships k3s via home-manager/programs/k8s on Linux so kubectl works without sudo.

  • New Features

    • Render k3s.service to ~/.config/k3s/k3s.service and install it to /etc/systemd/system/k3s.service; reload, enable, and start (idempotent via diff).
    • Copy /etc/rancher/k3s/k3s.yaml to ~/.kube/config with correct ownership and 600 perms.
    • Apply service and kubeconfig setup only on kyber; other hosts unchanged.
    • Add k3s to programs/k8s for Linux hosts.
  • Refactors

    • Move unit template and rendering to config/k3s using pkgs.replaceVars; service reads the rendered file instead of rendering it.
    • Fold kubeconfig copy into the k3s service activation and remove the separate kubeconfig module.

Written for commit 97d0297. Summary will update on new commits.

Manages k3s as a Nix-declared system service on kyber instead of requiring
a manual curl install. Splits concerns across config/kube (kubeconfig)
and services/k3s (systemd service).

Co-Authored-By: Claude Opus 4.6 <noreply@anthropic.com>
@mesa-dot-dev

mesa-dot-dev Bot commented Apr 26, 2026

Copy link
Copy Markdown

You do not have enough credits to review this pull request. Please purchase more credits to continue.

@coderabbitai

coderabbitai Bot commented Apr 26, 2026

Copy link
Copy Markdown

Caution

Review failed

Pull request was closed or merged during review

📝 Walkthrough

Summary by CodeRabbit

  • New Features
    • K3s now automatically installs on Linux systems
    • K3s runs as a managed systemd service with automatic restart and resource limit configuration
    • Kubernetes configuration automatically set up in user home directory with secure permissions
    • Service cleanup and network dependency management configured

Walkthrough

This pull request introduces a new K3s service module for Home Manager with conditional deployment on Linux systems. Changes include a systemd service unit template, activation logic for unit installation and systemd reloading, kubeconfig provisioning, and conditional K3s package installation alongside Kubernetes tooling.

Changes

Cohort / File(s) Summary
K3s Configuration Infrastructure
config/k3s/default.nix, config/k3s/k3s.service
Removes config parameter from module; introduces new systemd service unit with Type=notify, network dependencies, resource limits, and pre/post execution hooks for K3s server startup and cleanup.
Home Manager K8s Integration
home-manager/programs/k8s/default.nix
Adds lib parameter and refactors package list to conditionally append k3s on Linux systems using lib.optionals.
Home Manager Services Infrastructure
home-manager/services/default.nix, home-manager/services/k3s/default.nix
Registers new K3s service module; implements activation logic that deploys the systemd unit, reloads systemd, and provisions kubeconfig to ~/.kube/config with ownership and permissions management.

Sequence Diagram(s)

sequenceDiagram
    participant Activation as Home Manager<br/>Activation
    participant FileSystem as File System
    participant Systemd as Systemd
    
    Activation->>FileSystem: Generate ~/.config/k3s/k3s.service<br/>(from template)
    Activation->>FileSystem: Compare with /etc/systemd/system/k3s.service
    alt File differs or missing
        Activation->>FileSystem: Copy unit to /etc/systemd/system/
        Activation->>Systemd: Reload systemd daemon
        Activation->>Systemd: Enable k3s service
        Activation->>Systemd: Start k3s service
    end
    
    Activation->>FileSystem: Check /etc/rancher/k3s/k3s.yaml
    alt Kubeconfig exists
        Activation->>FileSystem: Ensure ~/.kube directory
        Activation->>FileSystem: Copy to ~/.kube/config
        Activation->>FileSystem: Set ownership & 0600 permissions
    end
Loading

Estimated code review effort

🎯 3 (Moderate) | ⏱️ ~25 minutes

Possibly related PRs

Poem

🐰 A server runs on Linux fine,
With k3s.service all in line,
Systemd reloads, kubeconfig flies,
Activation hooks—oh, what a prize!
Now Kubernetes dreams materialize!

🚥 Pre-merge checks | ✅ 5
✅ Passed checks (5 passed)
Check name Status Explanation
Title check ✅ Passed The title accurately summarizes the main changes: adding k3s systemd service and kubeconfig management for the kyber host, which is the primary focus of the changeset.
Description check ✅ Passed The description is directly related to the changeset, providing clear details about the k3s service module, kubeconfig setup, and registration of modules, which matches the actual changes.
Docstring Coverage ✅ Passed No functions found in the changed files to evaluate docstring coverage. Skipping docstring coverage check.
Linked Issues check ✅ Passed Check skipped because no linked issues were found for this pull request.
Out of Scope Changes check ✅ Passed Check skipped because no linked issues were found for this pull request.

✏️ Tip: You can configure your own custom pre-merge checks in the settings.

✨ Finishing Touches
🧪 Generate unit tests (beta)
  • Create PR with unit tests
  • Commit unit tests in branch feat/k3s-service

Thanks for using CodeRabbit! It's free for OSS, and your support helps us grow. If you like it, consider giving us a shout-out.

❤️ Share

Comment @coderabbitai help to get the list of available commands and usage tips.

@mesa-dot-dev

mesa-dot-dev Bot commented Apr 26, 2026

Copy link
Copy Markdown

Mesa Description

TL;DR

Adds a k3s systemd service and kubeconfig setup for the kyber host to enable Kubernetes management for non-root users.

What changed?

  • config/default.nix: Imports new Kubernetes-related configurations from the ./kube directory.
  • config/kube/activate.sh: New script to automate copying k3s kubeconfig to ~/.kube/config and setting correct ownership/permissions for non-root access.
  • config/kube/default.nix: New NixOS module introducing a Home Manager activation step to execute activate.sh conditionally on Kyber Linux systems.
  • home-manager/services/default.nix: Introduced a new k3s service configuration, making it available as a configurable service.
  • home-manager/services/k3s/default.nix: New Home Manager module to automate the setup and management of the K3s systemd service on Kyber Linux hosts.
  • config/k3s/k3s.service: New systemd service file configuring the K3s server, defining dependencies, resource limits, and lifecycle actions.
  • config/k3s/default.nix: Updated the deployment of k3s.service to use home.file and pkgs.replaceVars for dynamic injection of coreutils and k3s package paths; removed the config argument and some comments.
  • home-manager/programs/k8s/default.nix: Conditionally adds k3s to the home.packages list for Linux systems.

Description generated by Mesa. Update settings

@cubic-dev-ai cubic-dev-ai Bot left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

No issues found across 6 files

Co-Authored-By: Claude Opus 4.6 <noreply@anthropic.com>

@gemini-code-assist gemini-code-assist Bot left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Code Review

This pull request introduces k3s service management and automated kubeconfig synchronization for the user environment. It includes a new systemd service for k3s and activation scripts to ensure the kubeconfig is accessible for non-root kubectl usage. Review feedback suggests maintaining alphabetical order in module imports, implementing idempotency checks in the kubeconfig sync script to prevent unnecessary sudo prompts, and addressing inconsistencies in sudo detection and service restart behavior within the k3s setup.

Comment thread config/default.nix Outdated
./hammerspoon
./jj
./k3s
./kube

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

medium

The module list should maintain alphabetical order. ./kube should come after ./karabiner.

  ./karabiner
  ./kube

Comment thread config/kube/activate.sh Outdated
Comment on lines +21 to +26
if [ -n "$SUDO_CMD" ]; then
mkdir -p "$KUBE_DIR"
$SUDO_CMD cp "$K3S_KUBECONFIG" "$KUBE_DIR/config"
$SUDO_CMD chown "$(id -u):$(id -g)" "$KUBE_DIR/config"
chmod 600 "$KUBE_DIR/config"
fi

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

medium

The script currently overwrites the kubeconfig on every activation, which can trigger unnecessary sudo password prompts. Adding an idempotency check using diff (similar to the pattern used in the k3s service activation) would be more efficient. Additionally, if sudo is not found, the script fails gracefully with a warning to improve debuggability.

Suggested change
if [ -n "$SUDO_CMD" ]; then
mkdir -p "$KUBE_DIR"
$SUDO_CMD cp "$K3S_KUBECONFIG" "$KUBE_DIR/config"
$SUDO_CMD chown "$(id -u):$(id -g)" "$KUBE_DIR/config"
chmod 600 "$KUBE_DIR/config"
fi
if [ -n "$SUDO_CMD" ]; then
mkdir -p "$KUBE_DIR"
if [ ! -f "$KUBE_DIR/config" ] || ! $SUDO_CMD diff -q "$K3S_KUBECONFIG" "$KUBE_DIR/config" >/dev/null 2>&1; then
$SUDO_CMD cp "$K3S_KUBECONFIG" "$KUBE_DIR/config"
$SUDO_CMD chown "$(id -u):$(id -g)" "$KUBE_DIR/config"
chmod 600 "$KUBE_DIR/config"
fi
else
echo "Warning: sudo not found, skipping kubeconfig sync" >&2
fi
References
  1. Maintain consistency with established patterns for writing scripts that are extracted from Nix expressions.

Comment thread home-manager/services/k3s/default.nix Outdated
Comment on lines +24 to +26
$DRY_RUN_CMD sudo cp "${k3sServiceFile}" /etc/systemd/system/k3s.service
$DRY_RUN_CMD sudo ${pkgs.systemd}/bin/systemctl daemon-reload
$DRY_RUN_CMD sudo ${pkgs.systemd}/bin/systemctl enable --now k3s

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

medium

This activation script uses sudo directly, which is inconsistent with the detection logic implemented in config/kube/activate.sh. For better compatibility (e.g., on NixOS where the sudo wrapper is at a specific path), consider using a consistent detection method or ensuring sudo is in the PATH. Also, note that systemctl enable --now will not restart the service if the unit file has changed; if a configuration update requires a service restart, you may want to add $DRY_RUN_CMD sudo ${pkgs.systemd}/bin/systemctl restart k3s inside this block.

References
  1. Maintain consistency with established patterns for writing scripts that are extracted from Nix expressions.

shunkakinoki and others added 3 commits April 26, 2026 19:34
Config module owns the replaceVars rendering and exposes the result
via modules.k3s.serviceFile. Service module just consumes it.

Co-Authored-By: Claude Opus 4.6 <noreply@anthropic.com>
- k3s package available on all Linux via programs/k8s
- config/k3s renders k3s.service to ~/.config/k3s/ via home.file
- services/k3s just reads the rendered file and installs it (kyber-only)

Co-Authored-By: Claude Opus 4.6 <noreply@anthropic.com>
The kubeconfig depends on k3s running, so it belongs in the service
activation alongside the systemd setup.

Co-Authored-By: Claude Opus 4.6 <noreply@anthropic.com>
@shunkakinoki
shunkakinoki merged commit 3abd2e2 into main Apr 26, 2026
24 of 33 checks passed
@shunkakinoki
shunkakinoki deleted the feat/k3s-service branch April 26, 2026 19:40
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant