From c17d64d64625cd11bc76ae79d242b49ba1e6580b Mon Sep 17 00:00:00 2001 From: Adam Dinwoodie Date: Sat, 9 Aug 2025 11:30:01 +0100 Subject: [PATCH] .gitattributes: manage CRLF handling (#424336) In certain circumstances, Git will munge line endings when it checks out and commits files. This can result in difficult-to-debug errors when files are changed when they're checked out. To avoid that problem, set .gitattributes such that Git will always use LF line endings for files it detects as text files. *.diff and *.patch files are excluded, as committed patch files may need to patch upstream source code that intentionally uses CRLF line endings. Fixes #423762 --- .gitattributes | 25 +++++++++++++++++++++++++ 1 file changed, 25 insertions(+) diff --git a/.gitattributes b/.gitattributes index 32cd96ef9e2c..6f006049e92d 100644 --- a/.gitattributes +++ b/.gitattributes @@ -18,3 +18,28 @@ nixos/modules/module-list.nix merge=union # pkgs/top-level/all-packages.nix merge=union ci/OWNERS linguist-language=CODEOWNERS + +# Avoid munging line endings when using Git for Windows, and instead keep files +# using LF line endings. This particularly affects scripts committed in the +# nixpkgs repository. +# +# - `text` without `=auto` would mean "Git should always munge line endings on +# this file so there will never be a CRLF in the repository, and the line +# endings in the working directory should respect the local Git +# configuration." +# - `text=auto` means "Git should try to work out if this file is a text file. +# If it is, it should do the line-ending munging as for `text`, and if it +# isn't, it should leave the file alone." +# - `eol=lf` means "Ignore any local configuration about how line +# endings normally work on this platform. This file should always and only +# have LF line endings in the repo (so if there's a CR in the repo, it's +# meant to be there in addition to any end-of-line mark), and the selected +# attribute is how the file should appear in the working directory." +# +# See https://github.com/NixOS/nixpkgs/issues/423762 for historical context. +* text=auto eol=lf + +# Don't force LF line endings for diff/patch files, as they might be correctly +# patching CRLF line endings from an upstream source package. +*.diff !text !eol +*.patch !text !eol