nixos-config/packages/fuckport.nix
Sridhar Ratnakumar 87441d5270 stuf
2026-05-27 21:29:04 -04:00

40 lines
1,000 B
Nix

# https://x.com/sridca/status/1861875950897578113
{ writers, haskellPackages, coreutils, jc, lsof, ... }:
writers.writeHaskellBin "fuckport"
{
libraries = with haskellPackages; [
shh
aeson
];
} ''
{-# LANGUAGE TemplateHaskell #-}
{-# LANGUAGE DerivingStrategies #-}
{-# LANGUAGE DeriveAnyClass #-}
import Shh
import System.Environment
import Data.Aeson
import Data.Maybe
import GHC.Generics
import Control.Monad
loadFromBins ["${lsof}", "${coreutils}", "${jc}"]
data LsofRow = LsofRow
{ command :: String
, pid :: Int
, user :: String
}
deriving stock (Generic, Show)
deriving anyclass (FromJSON)
main :: IO ()
main = do
ports <- getArgs
forM_ ports $ \port -> do
s <- lsof "-i" (":" <> port) |> jc "--lsof" |> capture
let v = fromJust $ decode @[LsofRow] s
forM_ v $ \r -> do
putStrLn $ "Killing " <> show (pid r) <> " (" <> command r <> ") on port " <> port
kill ["-KILL", show (pid r)]
''