Merge pull request 'add manage.hs to replace Justfile' (#30) from manage.hs into trunk

Reviewed-on: https://codeberg.org/leana8959/.files/pulls/30
This commit is contained in:
Léana 2026-01-31 10:47:43 +01:00
commit 6e46babd4d
7 changed files with 138 additions and 38 deletions

View file

@ -1,34 +0,0 @@
# Note:
# add `--option substitute false` and or `--no-net` to the command so it builds properly offline
os host action:
#!/usr/bin/env bash
set -euo pipefail
nixpkgs=$(nix-instantiate --eval -E "let sources = import ./npins; in sources.nixpkgs.outPath" | jq -r .)
nixos-rebuild {{ action }} \
-I nixpkgs=${nixpkgs} \
-I nixos-config=./nix/configurations/{{ host }}.nix \
--no-reexec \
--file ./default.nix \
--attr "nixosConfigurations.{{ host }}"
install host:
#!/usr/bin/env bash
set -euo pipefail
nixpkgs=$(nix-instantiate --eval -E "let sources = import ./npins; in sources.nixpkgs.outPath" | jq -r .)
nixos-install \
-I nixpkgs=${nixpkgs} \
-I nixos-config=./nix/configurations/{{ host }}.nix \
--file ./default.nix \
--attr "nixosConfigurations.{{ host }}"
# Retain four weeks of generations so I don't fuck up
clean-os:
nix-env --delete-generations 28d -p /nix/var/nix/profiles/system
clean-hm:
nix-env --delete-generations 28d -p ~/.local/state/nix/profiles/home-manager
update:
npins update

View file

@ -56,6 +56,7 @@ in
environment.systemPackages = [
pkgs.disko
pkgs.manage
pkgs.git
pkgs.pastebinit # for sharing cli output & debugging
pkgs.hdparm # to ATA secure wipe disks

View file

@ -0,0 +1,10 @@
{
haskellPackages,
haskell,
}:
let
inherit (haskell.lib.compose) justStaticExecutables;
drv = haskellPackages.callCabal2nix "manage" ./src { };
in
justStaticExecutables drv

View file

@ -0,0 +1 @@
dist-newstyle/

View file

@ -0,0 +1,101 @@
#!/usr/bin/env runhaskell
{-# LANGUAGE LambdaCase #-}
{-# LANGUAGE ViewPatterns #-}
import System.Environment
import System.Exit
import System.IO
import System.Process
-- Note: to type check this code
-- `ghc manage.hs -Werror=all -fno-code'
main :: IO ()
main = do
let showHelpOr args elseDo =
if "--help" `elem` args then putStr help
else elseDo
getArgs >>= \case
( "os" : host : action
: (partitionArgs -> (manageArgs, extraArgs))
) ->
manageArgs `showHelpOr` do
nixpkgs <- readNixpkgsPath
-- NOTE: It seems like nom is able to do some pty magic and can print out to terminal in real time.
-- I thought this would be blocking?
readProcessFriendly "nixos-rebuild" (action : nixosRebuildArgs nixpkgs host <> extraArgs) ""
>>= putStr
( "install" : host
: (partitionArgs -> (manageArgs, extraArgs))
) ->
manageArgs `showHelpOr` do
nixpkgs <- readNixpkgsPath
readProcessFriendly "nixos-install" (nixosInstallArgs nixpkgs host <> extraArgs) ""
>>= putStr
_ -> putStr help >> exitFailure
newtype StorePath = StorePath { unStorePath :: String }
defaultNixosCmdArgs :: StorePath -> String -> [String]
defaultNixosCmdArgs (unStorePath -> nixpkgsPath) hostname =
[ "-I", "nixpkgs=" <> nixpkgsPath
, "-I", "nixos-config=./nix/configurations/" <> hostname <> ".nix"
, "--file", "./default.nix"
, "--attr", "nixosConfigurations." <> hostname
]
nixosInstallArgs :: StorePath -> String -> [String]
nixosInstallArgs = defaultNixosCmdArgs
nixosRebuildArgs :: StorePath -> String -> [String]
nixosRebuildArgs =
defaultNixosCmdArgs
-- This is not a nixos-install flag
<> (\_ _ -> ["--no-reexec"])
help :: String
help =
unlines
[ "Manage.hs"
, ""
, "Manage.hs is a thin wrapper to make nixos-{install,rebuild} easier to use."
, "Black lives matter. Trans rights are human rights. No nazi bullsh*t."
, ""
, "Available commands:"
, indent "os <hostname> <action>:" <> indent "run perhost action with nixos-rebuild"
, indent "install <hostname>:" <> indent "run perhost action with nixos-install"
, indent "--help:" <> indent "show this help menu"
]
where indent = ("\t" <>)
partitionArgs :: [String] -> ([String], [String])
partitionArgs = finalize . span (/= "--")
where finalize (xs, ys) = (xs, drop 1 ys)
quote :: String -> String
quote x = "\"" <> x <> "\""
-- https://stackoverflow.com/a/70162369
blueForeground :: String -> String
blueForeground x = "\ESC[34m" <> x <> "\ESC[0m"
readProcessFriendly :: String -> [String] -> String -> IO String
readProcessFriendly x args stdin_ = do
hPutStrLn stderr
$ "Executing: " <> (quote . blueForeground) (showCommandForUser x args)
readProcess x args stdin_
readNixpkgsPath :: IO StorePath
readNixpkgsPath =
StorePath . clean <$> readProcessFriendly
"nix-instantiate"
[ "--eval"
, "-E"
, "let sources = import ./npins; in sources.nixpkgs.outPath"
]
""
where
clean = filter (\c -> c /= '\n' && c /= '"')

View file

@ -0,0 +1,21 @@
cabal-version: 3.0
name: manage
version: 0.1.0.0
description: NixOS Helper
author: Léana 江
maintainer: leana.jiang+git@icloud.com
build-type: Simple
common common
ghc-options:
-Wall -Wcompat -Widentities -Wincomplete-record-updates
-Wincomplete-patterns -Wincomplete-uni-patterns
-Wredundant-constraints -Werror=missing-fields
build-depends: base
default-language: Haskell2010
executable manage
import: common
main-is: ./Main.hs
build-depends:
process

View file

@ -6,6 +6,7 @@ in
overlays = map import [
./nix/overlays/disko.nix
./nix/overlays/npins.nix
./nix/packages/overlay.nix
];
},
withGHC ? false,
@ -17,11 +18,10 @@ pkgs.mkShell {
packages =
with pkgs;
[
just
jq
npins
npins # from npins repo
disko # from disko flake
manage
nixos-anywhere # comes from nixpkgs
disko # comes from disko flake
]
++ lib.optionals withGHC [
(haskellPackages.ghcWithPackages (self: [