Hi everyone! I’m pretty inexperienced with NixOS and was struggling to make an overlay or flake to compile ffmpeg with the --extra-cflags="-some -flags" option (plus a couple other feature flags).
I was a little lost going through the nixpkgs ffmpeg config. I see the configureFlags list where I probably want to put this flag, but since it’s inside the stdenv.mkDerivation block, the override or flake doesn’t seem to have that variable in scope.
For example, if I put configureFlags in my overrides, I get an error like error: function 'anonymous lambda' called with unexpected argument 'configureFlags'. I wrote about this in more detail in this stack overflow question.
Is there a way to modify/override things in the package’s stdenv.mkDerivation scope?
If you don’t mind me asking another question in the same thread, it seems like now I’m running into an issue with double-quotes getting escaped, even when surrounded by two single quotes:
Even if I backslash-escape the double-quotes, something along the way then escapes my escape characters. e.g. configureFlags = old.configureFlags ++ [ ''--extra-cflags=\"-O3 -pipe -march=znver3 -ffat-lto-objects -ftree-loop-distribute-patterns\"'' ]; yields an error in the configure phase that transformed it into --extra-cflags=\\\"-O3 -pipe -march=znver3 -ffat-lto-objects -ftree-loop-distribute-patterns\\\"
I thought the two single quotes would prevent escaping like that occurring, so I’m not really sure why this is happening.
You cannot currently use spaces in configureFlags and most other such arguments. The array will be turned into an environment variable by Nix by joining it with spaces:
nix-repl> lib.traceVal (builtins.toString ["foo" "bar" ''\"with space\"''])
trace: foo bar \"with space\"
"foo bar \\\"with space\\\""
You need to use configureFlagsArray in preConfigure instead:
Or enable __structuredAttrs to be able to pass configureFlags as an actual array. But then you should remove the quotes.
Example comparing flags with __structuredAttrs and without
$ nix-build test.nix -I nixpkgs=$PWD
this derivation will be built:
/nix/store/vbfp311nvb5plnizra631yn25wig1a7f-foo.drv
building '/nix/store/vbfp311nvb5plnizra631yn25wig1a7f-foo.drv'...
structuredAttrs is enabled
*foo*
*bar*
*\"with space\"*
/nix/store/2ba05g7picfd7zmbxprfcs6h7waqsrwf-foo
$ nix-build test.nix -I nixpkgs=$PWD --arg structuredAttrs false
this derivation will be built:
/nix/store/d6978vj93mrin4h94b99h7a5j0dg52h6-foo.drv
building '/nix/store/d6978vj93mrin4h94b99h7a5j0dg52h6-foo.drv'...
*foo*
*bar*
*\"with*
*space\"*
/nix/store/rms2gl2ixsvrr3infk8f3czk2q81q6xp-foo
{ structuredAttrs ? true }:
let
pkgs = import <nixpkgs> {};
in
pkgs.runCommand "foo" {
__structuredAttrs = structuredAttrs;
fooFlags = [
"foo"
"bar"
''\"with space\"''
];
}
''
_accumFlagsArray fooFlags
for f in "''${flagsArray[@]}"; do
echo "*''${f}*"
done
touch "$out"
''
I think at some point I threw NIX_CFLAGS_COMPILE in there with hopes that ffmpeg would pick it up and never removed it. I can probably delete that line now.