A stable replacement for nix run in haskell
nix cli
6
fork

Configure Feed

Select the types of activity you want to include in your feed.

Initial commit

WeetHet 10ac142f

+396
+4
.envrc
··· 1 + watch_file package.yaml 2 + watch_file *.nix 3 + 4 + use nix
+15
.gitignore
··· 1 + * 2 + 3 + !.envrc 4 + !*.nix 5 + !package.yaml 6 + !*.cabal 7 + 8 + !LICENSE 9 + !.gitignore 10 + 11 + !app 12 + !app/*.hs 13 + 14 + !.zed 15 + !.zed/settings.json
+7
.zed/settings.json
··· 1 + { 2 + "languages": { 3 + "Haskell": { 4 + "tab_size": 2 5 + } 6 + } 7 + }
+29
LICENSE
··· 1 + Copyright (c) 2025, WeetHet 2 + 3 + 4 + Redistribution and use in source and binary forms, with or without 5 + modification, are permitted provided that the following conditions are met: 6 + 7 + * Redistributions of source code must retain the above copyright 8 + notice, this list of conditions and the following disclaimer. 9 + 10 + * Redistributions in binary form must reproduce the above 11 + copyright notice, this list of conditions and the following 12 + disclaimer in the documentation and/or other materials provided 13 + with the distribution. 14 + 15 + * Neither the name of the copyright holder nor the names of its 16 + contributors may be used to endorse or promote products derived 17 + from this software without specific prior written permission. 18 + 19 + THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS 20 + "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT 21 + LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR 22 + A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT 23 + HOLDER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, 24 + SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT 25 + LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, 26 + DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY 27 + THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT 28 + (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE 29 + OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
+221
app/Main.hs
··· 1 + module Main (main) where 2 + 3 + import Options.Applicative 4 + import Options.Applicative qualified as OA 5 + import System.Environment (withArgs) 6 + import System.IO (hPutStrLn) 7 + import System.Posix (executeFile) 8 + import System.Process (readProcess) 9 + 10 + data BuildOptions = BuildOptions 11 + { buildFileish :: Maybe Text, 12 + buildPackage :: Maybe Text, 13 + buildArgs :: [(Text, Text)], 14 + buildArgStrs :: [(Text, Text)], 15 + buildAttrs :: [Text], 16 + buildExprs :: [Text], 17 + buildDryRun :: Bool, 18 + buildIncludePaths :: [String], 19 + buildOptions :: [(Text, Text)], 20 + buildRepair :: Bool, 21 + buildNoBuildOutput :: Bool, 22 + buildMaxJobs :: Maybe Text, 23 + buildCores :: Maybe Int, 24 + buildMaxSilentTime :: Maybe Int, 25 + buildTimeout :: Maybe Int, 26 + buildKeepGoing :: Bool, 27 + buildKeepFailed :: Bool, 28 + buildFallback :: Bool, 29 + buildReadonlyMode :: Bool, 30 + buildVerbose :: Int, 31 + buildQuiet :: Int, 32 + buildLogFormat :: Maybe String, 33 + buildCheck :: Bool 34 + } 35 + deriving (Show) 36 + 37 + buildOptionsParser :: Parser BuildOptions 38 + buildOptionsParser = 39 + BuildOptions 40 + <$> optional (strArgument (metavar "fileish")) 41 + <*> optional 42 + ( strOption 43 + ( long "package" 44 + <> short 'p' 45 + <> metavar "package" 46 + <> help "Build a package by name (conflicts with fileish argument)" 47 + ) 48 + ) 49 + <*> many 50 + ( (,) 51 + <$> strOption (long "arg" <> metavar "name" <> help "Pass a value for argument 'name'") 52 + <*> strOption (metavar "value" <> help "Value for argument") 53 + ) 54 + <*> many 55 + ( (,) 56 + <$> strOption (long "argstr" <> metavar "name" <> help "Pass a string value for argument 'name'") 57 + <*> strOption (metavar "value" <> help "String value for argument") 58 + ) 59 + <*> many 60 + ( strOption 61 + ( long "attr" 62 + <> short 'A' 63 + <> metavar "attrPath" 64 + <> help "Select attribute to build" 65 + ) 66 + ) 67 + <*> many 68 + ( strOption 69 + ( long "expr" 70 + <> short 'E' 71 + <> metavar "expr" 72 + <> help "Interpret argument as a Nix expression" 73 + ) 74 + ) 75 + <*> switch (long "dry-run" <> help "Show what would be built, without building") 76 + <*> many 77 + ( strOption 78 + ( long "include" 79 + <> short 'I' 80 + <> metavar "[name=]path" 81 + <> help "Add a path to the Nix expression search path" 82 + ) 83 + ) 84 + <*> many 85 + ( (,) 86 + <$> strOption (long "option" <> metavar "name" <> help "Set Lix configuration option") 87 + <*> strOption (metavar "value" <> help "Value for option") 88 + ) 89 + <*> switch (long "repair" <> help "Fix corrupted or missing store paths by redownloading or rebuilding them") 90 + <*> switch (long "no-build-output" <> short 'Q' <> help "Suppress output from builders to stderr") 91 + <*> optional 92 + ( strOption 93 + ( long "max-jobs" 94 + <> short 'j' 95 + <> metavar "number" 96 + <> help "Maximum number of build jobs (or 'auto')" 97 + ) 98 + ) 99 + <*> optional 100 + ( option 101 + auto 102 + ( long "cores" 103 + <> metavar "number" 104 + <> help "Number of CPU cores to use for building" 105 + ) 106 + ) 107 + <*> optional 108 + ( option 109 + auto 110 + ( long "max-silent-time" 111 + <> metavar "seconds" 112 + <> help "Maximum number of seconds a builder can go without producing output" 113 + ) 114 + ) 115 + <*> optional 116 + ( option 117 + auto 118 + ( long "timeout" 119 + <> metavar "seconds" 120 + <> help "Maximum number of seconds a builder can run" 121 + ) 122 + ) 123 + <*> switch (long "keep-going" <> short 'k' <> help "Keep going in case of failed builds") 124 + <*> switch (long "keep-failed" <> short 'K' <> help "Keep failed build directories") 125 + <*> switch (long "fallback" <> help "Fall back on building if substitutes fail") 126 + <*> switch (long "readonly-mode" <> help "Do not open the Lix database") 127 + <*> (length <$> many (flag' () (long "verbose" <> short 'v' <> help "Increase verbosity"))) 128 + <*> (length <$> many (flag' () (long "quiet" <> help "Decrease verbosity"))) 129 + <*> optional 130 + ( strOption 131 + ( long "log-format" 132 + <> metavar "format" 133 + <> help "Change the output log format (raw, internal-json, bar, bar-with-logs, multiline, multiline-with-logs)" 134 + ) 135 + ) 136 + <*> switch (long "check" <> help "Run the build in check mode (pass --check to nix-build)") 137 + 138 + verifyNoConflicts :: BuildOptions -> IO BuildOptions 139 + verifyNoConflicts opts = 140 + case (buildFileish opts, buildPackage opts) of 141 + (Just _, Just _) -> do 142 + hPutStrLn stderr "Error: --package/-p and fileish argument cannot be used together." 143 + exitFailure 144 + (Nothing, Nothing) -> do 145 + hPutStrLn stderr "Error: You must specify either a fileish argument or --package/-p." 146 + exitFailure 147 + _ -> pure opts 148 + 149 + nixBuildArgs :: BuildOptions -> [String] 150 + nixBuildArgs BuildOptions {..} = 151 + concat 152 + [ maybe [] (\f -> [toString f]) buildFileish, 153 + maybe [] (\p -> ["-A", toString p]) buildPackage, 154 + concatMap (\(n, v) -> ["--arg", toString n, toString v]) buildArgs, 155 + concatMap (\(n, v) -> ["--argstr", toString n, toString v]) buildArgStrs, 156 + concatMap (\a -> ["-A", toString a]) buildAttrs, 157 + concatMap (\e -> ["-E", toString e]) buildExprs, 158 + ["--dry-run" | buildDryRun], 159 + concatMap (\i -> ["-I", i]) buildIncludePaths, 160 + concatMap (\(n, v) -> ["--option", toString n, toString v]) buildOptions, 161 + ["--repair" | buildRepair], 162 + ["--no-build-output" | buildNoBuildOutput], 163 + maybe [] (\j -> ["-j", toString j]) buildMaxJobs, 164 + maybe [] (\c -> ["--cores", show c]) buildCores, 165 + maybe [] (\s -> ["--max-silent-time", show s]) buildMaxSilentTime, 166 + maybe [] (\t -> ["--timeout", show t]) buildTimeout, 167 + ["--keep-going" | buildKeepGoing], 168 + ["--keep-failed" | buildKeepFailed], 169 + ["--fallback" | buildFallback], 170 + ["--readonly-mode" | buildReadonlyMode], 171 + replicate buildVerbose "--verbose", 172 + replicate buildQuiet "--quiet", 173 + maybe [] (\fmt -> ["--log-format", fmt]) buildLogFormat, 174 + ["--check" | buildCheck] 175 + ] 176 + 177 + main :: IO () 178 + main = do 179 + args <- getArgs 180 + let (ours, rest) = break (== "--") args 181 + let passthrough = maybe [] tail (nonEmpty rest) 182 + 183 + withArgs ours $ do 184 + opts_ <- 185 + execParser $ 186 + info 187 + (buildOptionsParser <**> helper) 188 + (fullDesc <> progDesc "nix-run - run a Nix application" <> OA.header "nnix-run - run a Nix ") 189 + opts <- verifyNoConflicts opts_ 190 + 191 + let file = case buildPackage opts of 192 + Just _ -> "<nixpkgs>" 193 + Nothing -> fromMaybe "./." (buildFileish opts) 194 + 195 + let attr = case (buildAttrs opts, buildPackage opts) of 196 + (a : _, _) -> Just a 197 + ([], Just p) -> Just p 198 + ([], Nothing) -> Nothing 199 + 200 + let attrSub = maybe "" ("." <>) attr 201 + let expr = 202 + unlines 203 + [ "let", 204 + " pkgs = import <nixpkgs> {};", 205 + " drv = import (" <> file <> ");", 206 + " drvResolved = if builtins.isFunction drv then drv {} else drv;", 207 + "in pkgs.lib.getExe drvResolved" <> attrSub 208 + ] 209 + binPath <- 210 + readProcess 211 + "nix-instantiate" 212 + ["--eval", "--raw", "-E", toString expr] 213 + "" 214 + let attrArgs = maybe [] (("-A" :) . one) attr 215 + _ <- 216 + readProcess 217 + "nix-build" 218 + ((++ nixBuildArgs opts) . map toString $ ["--no-out-link", file] ++ attrArgs) 219 + "" 220 + 221 + executeFile binPath False passthrough Nothing
+47
nix-run-hs.cabal
··· 1 + cabal-version: 2.2 2 + 3 + -- This file has been generated from package.yaml by hpack version 0.38.2. 4 + -- 5 + -- see: https://github.com/sol/hpack 6 + 7 + name: nix-run-hs 8 + version: 0.1.0.0 9 + category: System 10 + homepage: https://tangled.org/@weethet.bsky.social/nix-run 11 + author: WeetHet 12 + maintainer: stas.ale66@gmail.com 13 + license: BSD-3-Clause 14 + license-file: LICENSE 15 + build-type: Simple 16 + 17 + executable nix-run-hs 18 + main-is: Main.hs 19 + other-modules: 20 + Paths_nix_run_hs 21 + autogen-modules: 22 + Paths_nix_run_hs 23 + hs-source-dirs: 24 + app 25 + default-extensions: 26 + ApplicativeDo 27 + DataKinds 28 + DerivingStrategies 29 + GADTs 30 + LambdaCase 31 + OverloadedRecordDot 32 + OverloadedStrings 33 + RankNTypes 34 + RecordWildCards 35 + TypeApplications 36 + ghc-options: -Wall -Wall 37 + build-depends: 38 + base >=4.20.2.0 && <4.21 39 + , optparse-applicative >=0.18.1.0 && <0.19 40 + , process 41 + , relude >=1.2.2.0 && <1.3 42 + , unix 43 + mixins: 44 + base hiding (Prelude) 45 + , relude (Relude as Prelude) 46 + , relude 47 + default-language: GHC2024
+41
package.yaml
··· 1 + name: nix-run-hs 2 + version: 0.1.0.0 3 + homepage: https://tangled.org/@weethet.bsky.social/nix-run 4 + license: BSD-3-Clause 5 + license-file: LICENSE 6 + author: WeetHet 7 + maintainer: stas.ale66@gmail.com 8 + category: System 9 + build-type: Simple 10 + 11 + ghc-options: -Wall 12 + 13 + executables: 14 + nix-run-hs: 15 + main: Main.hs 16 + source-dirs: app 17 + ghc-options: -Wall 18 + dependencies: 19 + - name: base 20 + version: "^>=4.20.2.0" 21 + mixin: hiding (Prelude) 22 + - name: relude 23 + version: "^>= 1.2.2.0" 24 + mixin: 25 + - (Relude as Prelude) 26 + - "" 27 + - optparse-applicative ^>= 0.18.1.0 28 + - process 29 + - unix 30 + language: GHC2024 31 + default-extensions: 32 + - ApplicativeDo 33 + - DataKinds 34 + - DerivingStrategies 35 + - GADTs 36 + - LambdaCase 37 + - OverloadedRecordDot 38 + - OverloadedStrings 39 + - RankNTypes 40 + - RecordWildCards 41 + - TypeApplications
+32
shell.nix
··· 1 + { 2 + system ? builtins.currentSystem, 3 + pkgs ? import <nixpkgs> { inherit system; }, 4 + }: 5 + pkgs.haskellPackages.shellFor { 6 + packages = hpkgs: [ 7 + ((hpkgs.callCabal2nix "nix-run" ./. { }).overrideAttrs (old: { 8 + preConfigure = '' 9 + if [ -f package.yaml ] && [ ! -f nix-run-hs.cabal ]; then 10 + ${pkgs.haskellPackages.hpack}/bin/hpack 11 + fi 12 + ''; 13 + 14 + nativeBuildInputs = (old.nativeBuildInputs or [ ]) ++ [ pkgs.haskellPackages.hpack ]; 15 + })) 16 + ]; 17 + 18 + withHoogle = true; 19 + 20 + nativeBuildInputs = [ 21 + pkgs.cabal-install 22 + pkgs.hlint 23 + pkgs.hpack 24 + pkgs.haskell-language-server 25 + pkgs.haskellPackages.cabal-gild 26 + pkgs.haskellPackages.ormolu 27 + 28 + pkgs.npins 29 + ]; 30 + 31 + shellHook = "hpack"; 32 + }