Commit 54ad3044138d

Vincent Demeester <vincent@sbr.pm>
2025-03-10 12:36:53
users/vincent: fix pythonrc ๐Ÿ˜‡
Signed-off-by: Vincent Demeester <vincent@sbr.pm>
1 parent 319af68
Changed files (2)
users
vincent
users/vincent/dev/python/pythonrc
@@ -0,0 +1,36 @@
+#!/usr/bin/env python3
+# This entire thing is unnecessary post v3.13.0a3
+# https://github.com/python/cpython/issues/73965
+
+def is_vanilla() -> bool:
+    """ :return: whether running "vanilla" Python <3.13 """
+    import sys
+    return not hasattr(__builtins__, '__IPYTHON__') and 'bpython' not in sys.argv[0] and sys.version_info < (3, 13)
+
+
+def setup_history():
+    """ read and write history from state file """
+    import os
+    import atexit
+    import readline
+    from pathlib import Path
+
+    # Check PYTHON_HISTORY for future-compatibility with Python 3.13
+    if history := os.environ.get('PYTHON_HISTORY'):
+        history = Path(history)
+    # https://specifications.freedesktop.org/basedir-spec/basedir-spec-latest.html#variables
+    elif state_home := os.environ.get('XDG_STATE_HOME'):
+        state_home = Path(state_home)
+    else:
+        state_home = Path.home() / '.local' / 'state'
+
+    history: Path = history or state_home / 'python_history'
+
+    # https://github.com/python/cpython/issues/105694
+    if not history.is_file():
+        readline.write_history_file(str(history)) # breaks on macos + python3 without this.
+    readline.read_history_file(history)
+    atexit.register(readline.write_history_file, history)
+
+    if is_vanilla():
+        setup_history()
users/vincent/dev/python.nix
@@ -1,46 +1,7 @@
 { config, pkgs, ... }:
 
 {
-  xdg.configFile."python/pythonrc".text = ''
-    #!/usr/bin/env python3                                                                                            
-    # This entire thing is unnecessary post v3.13.0a3                                                                 
-    # https://github.com/python/cpython/issues/73965                                                                  
-                                                                                                                  
-    def is_vanilla() -> bool:                                                                                         
-        """ :return: whether running "vanilla" Python <3.13 """                                                       
-        import sys                                                                                                    
-        return not hasattr(__builtins__, '__IPYTHON__') and 'bpython' not in sys.argv[0] and sys.version_info < (3, 13)                                                                                        
-                                                                                                                  
-                                                                                                                  
-    def setup_history():                                                                                              
-        """ read and write history from state file """                                                                
-        import os                                                                                                     
-        import atexit                                                                                                 
-        import readline                                                                                               
-        from pathlib import Path                                                                                      
-                                                                                                                  
-        # Check PYTHON_HISTORY for future-compatibility with Python 3.13                                              
-        if history := os.environ.get('PYTHON_HISTORY'):                                                               
-            history = Path(history)                                                                                   
-        # https://specifications.freedesktop.org/basedir-spec/basedir-spec-latest.html#variables                      
-        elif state_home := os.environ.get('XDG_STATE_HOME'):                                                          
-            state_home = Path(state_home)                                                                             
-        else:                                                                                                         
-            state_home = Path.home() / '.local' / 'state'                                                             
-                                                                                                                  
-        history: Path = history or state_home / 'python_history'                                                      
-                                                                                                                  
-        # https://github.com/python/cpython/issues/105694                                                             
-        if not history.is_file():                                                                                     
-            readline.write_history_file(str(history)) # breaks on macos + python3 without this.                       
-                                                                                                                  
-        readline.read_history_file(history)                                                                           
-        atexit.register(readline.write_history_file, history)                                                         
-                                                                                                                  
-                                                                                                                  
-        if is_vanilla():                                                                                                  
-            setup_history()
-  '';
+  xdg.configFile."python/pythonrc".source = ./python/pythonrc;
   home.packages = with pkgs; [
     pipenv
     python3