Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 2 additions & 2 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -291,9 +291,9 @@ to return back to the unmerged state.

## Usage

* `nixfmt < input.nix` – reads Nix code from `stdin`, formats it, and outputs to `stdout`
* `echo "{a=1;}" | nixfmt --stdin-filepath input.nix` – reads Nix code from `stdin`, formats it, and outputs to `stdout` (the filepath (`input.nix`) is only used for error messages)
* `nixfmt file.nix` – format the file in place

## Acknowledgements
## Acknowledgments

`nixfmt` was originally developed by [Serokell](https://github.com/serokell) and later donated to become an official Nix project with the acceptance of [RFC 166](https://github.com/NixOS/rfcs/pull/166).
17 changes: 8 additions & 9 deletions main/Main.hs
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,6 @@ import Data.ByteString.Char8 (unpack)
import Data.Either (lefts)
import Data.FileEmbed
import Data.List (intersperse, isSuffixOf)
import Data.Maybe (fromMaybe)
import Data.Text (Text)
import qualified Data.Text.IO as TextIO (getContents, hPutStr, putStr)
import Data.Version (showVersion)
Expand Down Expand Up @@ -57,7 +56,7 @@ data Nixfmt = Nixfmt
strict :: Bool,
verify :: Bool,
ast :: Bool,
filename :: Maybe FilePath,
stdin_filepath :: Maybe FilePath,
ir :: Bool
}
deriving (Show, Data, Typeable)
Expand Down Expand Up @@ -89,11 +88,11 @@ options =
False
&= help
"Pretty print the internal AST, only for debugging",
filename =
stdin_filepath =
Nothing
&= help
"The filename to display when the file input is given through stdin.\n\
\Useful for tools like editors and autoformatters that wish to use Nixfmt without providing it direct file access, while still providing context to where the file is.",
"Format the contents of stdin. When specified, no positional arguments are allowed.\n\
\The filepath is is only used for error messages.",
ir =
False
&= help
Expand Down Expand Up @@ -153,8 +152,8 @@ checkTarget format Target{tDoRead, tPath} = do
| formatted == contents -> Right ()
| otherwise -> Left $ tPath ++ ": not formatted"

stdioTarget :: Maybe FilePath -> Target
stdioTarget filename = Target TextIO.getContents (fromMaybe "<stdin>" filename) (const TextIO.putStr)
stdioTarget :: FilePath -> Target
stdioTarget filepath = Target TextIO.getContents filepath (const TextIO.putStr)

fileTarget :: FilePath -> Target
fileTarget path = Target (readFileUtf8 path) path atomicWriteFile
Expand All @@ -169,8 +168,8 @@ checkFileTarget :: FilePath -> Target
checkFileTarget path = Target (readFileUtf8 path) path (const $ const $ pure ())

toTargets :: Nixfmt -> IO [Target]
toTargets Nixfmt{files = [], filename} = pure [stdioTarget filename]
toTargets Nixfmt{files = ["-"], filename} = pure [stdioTarget filename]
toTargets Nixfmt{files = [], stdin_filepath = Just filepath} = pure [stdioTarget filepath]
Copy link
Collaborator Author

Choose a reason for hiding this comment

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

Suggested change
toTargets Nixfmt{files = [], stdin_filepath = Just filepath} = pure [stdioTarget filepath]
toTargets Nixfmt{files = [], stdin_filepath = Just filepath} = do
-- add a warning. https://github.com/NixOS/nixfmt/blob/b672593734d64eefb675b1f1de26f8a73fe00c40/main/Main.hs#L130
pure [stdioTarget filepath]

toTargets Nixfmt{files = _ : _, stdin_filepath = Just _} = error "Cannot specify both positional files and --stdin-filepath"
Copy link
Collaborator Author

Choose a reason for hiding this comment

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

This seems wrong to me. Should I instead change the return type of toTargets? Pointers appreciated.

toTargets Nixfmt{check = False, files = paths} = map fileTarget <$> collectAllNixFiles paths
toTargets Nixfmt{check = True, files = paths} = map checkFileTarget <$> collectAllNixFiles paths

Expand Down
13 changes: 9 additions & 4 deletions test/test.sh
Original file line number Diff line number Diff line change
@@ -1,5 +1,10 @@
#!/usr/bin/env bash

# We invoke `nixmft` in a way that makes shellcheck think we're clobbering the
# file we're formatting as we're reading it. That's not actually happening
# here.
# shellcheck disable=SC2094

set -euo pipefail

# Simple test runner for nixfmt.
Expand Down Expand Up @@ -35,7 +40,7 @@ verifyCorrect() {
shift

echo "Checking $file (${*:-no options}) …"
if ! out=$(nixfmt --strict --verify "$@" < "$file"); then
if ! out=$(nixfmt --strict --verify --stdin-filepath "$file" "$@" < "$file"); then
echo "[ERROR] failed nixfmt verification"
exit 1
fi
Expand All @@ -60,7 +65,7 @@ verifyCorrect test/correct-indent-4.nix --indent=4

# Verify "invalid"
for file in test/invalid/*.nix; do
if nixfmt < "$file" > /dev/null 2>&1; then
if nixfmt --stdin-filepath "test-$file" < "$file" > /dev/null 2>&1; then
echo "[ERROR] $file should have failed nixfmt"
exit 1
else
Expand All @@ -71,7 +76,7 @@ done
# Verify "diff"
for file in test/diff/**/in.nix; do
echo "Checking $file …"
out="$(nixfmt --verify < "$file")"
out="$(nixfmt --verify --stdin-filepath "$file" < "$file")"
outfile="$(dirname "$file")/out.nix"

if diff --color=always --unified "$outfile" <(echo "$out"); then
Expand All @@ -85,7 +90,7 @@ for file in test/diff/**/in.nix; do
fi

echo "Checking $file with --strict …"
out="$(nixfmt --strict --verify < "$file")"
out="$(nixfmt --strict --verify --stdin-filepath "$file" < "$file")"
outfile="$(dirname "$file")/out-pure.nix"

if diff --color=always --unified "$outfile" <(echo "$out"); then
Expand Down